I'm trying to write a program to manage some clients. Each client has reservations, and each reservation has items.I know that I have one to many relationship, but I cannot figure out how to do that.
I'm not using Spring, only JDBC.
Without a database I did it like that:
public class Client {
private String _fullName;
private String _address;
private String _email;
private String _phoneNumber;
private List<Reservation> _reservations;
}
public class Reservation {
private List<Item> _items;
private int _totalSum;
private boolean _toDeliver;
private String _date;
}
public class Item {
//primary key.
private int _id;
private int _price;
private String _name;
}
Do I have to declare these Lists? Is it possible to do that without Spring, Hibernate and other stuff? I just want to know how design my program.
Yes, you can do everything just with a JDBC database driver.
The first thing you have to do is to design your database, that is your tables and constraints and etc.
Then you go back to Java and query your database using the JDBC.
A common approach to this is creating a DAO class, where you can decouple your database query from your code. It goes something like this:
public String getUserName(String userId) throws SQLException {
String sql = "SELECT name FROM user WHERE user_id = ?";
try (Connection connection = getConnection();
PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, userId);
try (ResultSet rs = stmt.executeQuery()) {
String name = rs.getString("name");
return name;
}
}
}
This is just a simple example, there's no error handling here, neither dealing with empty query results, there are better examples online to illustrate how to make a DAO and create a connection. Depending on where you are running this code, you would prefer using a Connection Pool for efficiency (another thing to read more about).
In your use case, you would have the DAO get the data to make your Item, create a new Item and return it from the DAO. There are plenty of examples of this online, if you have further questions, don't hesitate to ask.
Related
So I have the first steps of a webapp, have class Doctor and I want to perform some operations like view all, insert, delete, etc. :
public class Doctor {
public String firstName;
public String lastName;
public int id;
public Doctor(){
}
public Doctor(int id, String first, String last){
setId(id);
setFirstName(first);
setLastName(last);
}
// getters and setters
Here is an implementation of one method from my interface Service. They are all pretty much the same with the appropriate sql queries. I tried following several different tutorials.
public class DAOImpl implements DAO{
public void insertUpdateDoctor(Doctor doctor){
String sql = "INSERT INTO doc_flight.docflight_doctors(id, first_name,last_name)" + "Values(?,?,?)";
jdbcTemplateObject.update(sql,new Object[]{doctor.getId(),doctor.getFirstName(),doctor.getLastName()});
Heres the part in main where I try to call it. The program doesn't even try to enter the method, it doesn't come up in debug and moves to the next method I try in main, view all, which works. Presumably, I'm not calling the method correctly and tried rewriting all parts several times. Help?!
Doctor test = new Doctor(17,"jack", "sparrow");
service.insertUpdateDoctor(test);
The issue itself it's not pretty clear for me.
If the problem is that when calling this:
Doctor test = new Doctor(17,"jack", "sparrow");
service.insertUpdateDoctor(test);
The runtime is not getting inside insertUpdateDoctor, just check how you are instantiating the object service
if the problem is that it's not executing correctly the sql statement, try by using a PreparedStatement (it's a good practice) by doing something like:
String connectionStr = StringUtils.format("INSERT INTO %s.docflight_doctors(id, first_name,last_name) Values(?,?,?)", this.databaseName);
try (Connection connection = this.dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(connectionStr)) {
preparedStatement.setInt(1, doctor.getId());
preparedStatement.setString(2, doctor.getFirstName());
preparedStatement.setString(3, doctor.getLastName());
preparedStatement.execute();
connection.commit();
} catch (Exception ex) {
this.logger.error(String.format("Error when inserting: %s", ex.toString()));
}
Hope it may help you.
For me I would not write this class from scratch, I would prefer to generate it in few clicks and save my time using The Cloud Wizard:
Go to https://codegen.cloud-wizard.com
Click on Java
From the technologies section press on Java SE
Select JDBC Class transformer.
In the metadata section enter a name for the JDBC Class e.g. (DoctorDao)
Add some fields e.g. first name and last name
Press on generate code and you will get your class ready and working as expected.
I want to know what is the correct way to return resultset in form of some Collection.
Or should I create class instances of same class and then return it (how to do that ?)
What is the common practice to do this? Or should I learn Hibernate and implement that?
public class Author {
private String table_name = "authors";
int id;
String full_name;
String location;
int age;
String details;
/** Getter Setter Methods here ....*/
/** Constructor here.... */
/* Returns all Authors */
public ArrayList all() {
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
/* TODO : figure out which collection is suitable .*/
DbHelper dbHelper = new DbHelper();
Connection conn = dbHelper.getConnection();
ResultSet rs = null;
Statement stmt = null;
try {
stmt = conn.createStatement();
String query = "SELECT * FROM authors";
rs = stmt.executeQuery(query);
while (rs.next()) {
HashMap<String, String> hashmap = new HashMap<>();
int id = rs.getInt(1);
hashmap.put("id",rs.getString(1));
hashmap.put("full_name", rs.getString(2));
hashmap.put("location", rs.getString(3));
hashmap.put("age", rs.getString(4));
hashmap.put("details", rs.getString(5));
list.add(id,hashmap);
//hashmap.clear();
}
} catch (SQLException ex) {
Logger.getLogger(Author.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("you are fuckt");
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException ex) {
Logger.getLogger(Author.class.getName()).log(Level.SEVERE, null, ex);
}
}
return list;
}
}
I would say you should go for hibernate. you will get the list of objects just by performing "list()" method.
code for your reference
List<Authors> al = this.sessionFactory.getCurrentSession().createQuery("from Authors").list();
Hibernare provides many other advantages too. Its definitely better option than JDBC.
JDBC alternative:
best practices says that u should return a DTO(Data transfer object) from DAO layer because it is advisable to get the relevant data in one go from DB so u can minimize the DB hits. Follow This:
List<YourDTO> al = new ArrayList<yourDTO>();
YourDTO is:
class YourDTO
{
private int id:
private Authors authors;
// getters and setters
}
read the data from resultSet as your doing then create the objects of Authors , set the fields , add them to ArrayList with id.
There are many good options available:
Spring JDBC is a very good choice for mostly read db apps.
Jooq is a one of the most advanced java querying alternatives, but it's free of charge only for open-source dbs.
MyBatis and QueryDSL have great sql querying features, but don't match Hibernate in the create/update/delete part.
Hibernate is great for domain driven models, especially for managing complex state changes, polymorphic types. It has great features like optimistic locking, dirty checks, transactional unit of works, first and second level caches, but it takes some time to master it having a steep learning curve.
Try Spring JDBC first and if that's not enough than invest your time and effort into Hibernate.
1) In my application we have a table which stores some settings detail which are used heavily for every functionality.Basically some conditions are set up in this table which are checked.
Now currently what we are doing is making DB call every now and then to fetch the details and check the condition.This has resulted in lot of DB calls and repetitive calls for the same condition.There are quite a lot of condition records in this table.We need to change this to make it perform better.
2) In my previous project also i had same requirement where we have a configuration table where such settings are configured,there we implemented it like load the configuration from table at start up of application and store them in System.setProperty(name,value)..and later use System.getProperty(name) to retrieve it.In case any settings is changed we update this in its System property.This worked like a charm.
However,in my current case(point 1 above) I don't think my prev way is best way to do it because lot of records present in the table having lot of columns.
Can anyone suggest a way how to achieve such kind of behavior in Java? Some of them I can think of is
1) Using System.setProperty like I explained.
2) Using caching like EHCache - standalone or with hibernate(as we are using hibernate).
Any better way to store such global settings of application so as to avoid DB calls?
Best way to do it using Ehcache(but I have never implemented it). And you should not play with System property it may cause conflict with other system properties.
How I did it in my case I have created a bean class which is having properties as yor table column like autoid, name, address, mobile.
public Map<Integer, PersonBean> getTag()
{
Map<Integer, PersonBean> map= new HashMap<Integer, PersonBean>();
Connection conn =getConnection();
Statement st=null;
ResultSet rs=null;
try
{
String sql = "select autoid,name, address, mobile from person";//person table
st = conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next()){
int tagid=rs.getInt("autoid");
String name=rs.getString("name");
String address=rs.getString("address");
String mobile=rs.getString("mobile");
PersonBean tagdeatilbean= new PersonBean(auotoid,name, address, mobile);
map.put(rs.getInt("autoid"), tagdeatilbean);
}
return map;
}
catch (SQLException e) {
}
finally
{
}
}
and bean class
public class PersonBean
{
private int autoid;
private String name;
private String address;
private String mobile;
public PersonBean(int autoid, String name, String address, String mobile)
{
this.autoid=autoid;
.....
....
}
//setter
//getter
}
If this is web application then you can load this cache map in init method and call the value according to autuid of row from map.
Well that's really embarrassing I have made a standard pojo class and its dao class for data retrieval purpose. I am having a difficulty to understand a basic procedure to how to handle a customized query data to Pojo class.
let's say my User class is
public class User{
private int userId;
private String username;
private int addressId;
}
public class Address{
private int addressId;
private String zip;
}
public class UserDAO{
public void getUserDetails(){
String getSql = select u.userId, u.username, a.zipcode from user u, address a where u.addressId = a.addressId;
//no pojo class is now specific to the resultset returned. so we can't map result to pojo object
}
}
now how I should model this with my pojo class as if using String to manage this then concept of object oriented vanishes, also complexity would increase in the future as well. kindly guide!
Update for Further Explanation
We know that we can map same table objects with same pojo class, but when the query is customized and there is a data returned which doesn't map to any specific class then what would be the procedure? i.e. should we make another class? or should we throw that data in a String variable? kindly give some example as well.
For this purpose you can use one of implementation of JPA. But as you want to do it manually I will give you small example.
UPD:
public class User {
private int userId;
private String username;
private Address address; // USE POJO not ID
}
public class Address{
private int addressId;
private String zip;
List<User> users;
}
public User getUserById(Connection con, long userId) {
PreparedStatement stmt;
String query = "select u.user_id, u.user_name, a.id, a.zip from user u, address a where a.address_id = u.id and u.id = ?";
User user = new User();
Address address = new Address;
try {
stmt = con.prepareStatement(query);
stmt.setLong(1, userId);
ResultSet rs = stmt.executeQuery();
address.setId(rs.getInt("id"));
address.setZip(rs.getString("zip");
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("user_name"));
user.setAddressId(rs.getInt("address_id"));
user.setAddress(address); // look here
} catch (SQLException e) {
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.rollback();
} catch (SQLException excep) {
}
}
} finally {
if (stmt != null) {
stmt.close();
}
}
return user;
}
You shouldn't do new POJO for that query, you should write normal query. And remember - your object model is main, tables in DB is just a way to save data of your application.
We know that we can map same table objects with same pojo class, but when the query is customized and there is a data returned which doesn't map to any specific class then what would be the procedure? i.e. should we make another class?
JPA dynamic instantiation allows you to define a query with a POJO whose constructor specifies only the fields and types you want from the database.
This will perform a JPA selection which will return a List.
If you need to change the query later and the columns are unchanged, your POJO will still work.
If you change the columns, then also change the POJO accordingly.
NOTE:
You must specify fully qualified package and constructor arguments.
Type User must be a JPA-mapped or JPA-annotated entity class.
The entityManager is in JPA EntityManagerFactory.
TypedQuery<User> q;
String sql = "select new com.stuff.User(
int u.userId, String u.username, String a.zipcode)
from User u, Address a where u.addressId = a.addressId";
List<User> list = entityManager.createQuery(sql).getResultList();
for(User u : list) {
doStuff(u);
}
Dynamic instantiation is also handy when you want to select specified columns, but avoid those columns with large data, such as BLOB types.
For example, maybe you want a list of proxy POJO's which represent the fully populated thing, but are themselves not fully populated.
You present the proxy list, and when the user selects one, then you do another query to get the fully populated object.
Your mileage may vary.
There's many ORM frameworks that can do this including Hibernate, myBatis, JPA and spring-JDBC
spring-jdbc and myBatis give you granular control over the SQL whereas with JPA and Hibernate you are usually abstracted away from the SQL.
I suggest you do some reading and figure out which one you like before rolling your own solution.
Your question:
We know that we can map same table objects with same pojo class,
but when the query is customized and there is a data returned
which doesn't map to any specific class then what would be the procedure?
If you have 100 kinds of SQL which returns different combination of columns, could it be to create 100 different POJOs? The answer is "NO, stop using POJO".
This library qood is designed to solve this problem, you can try it.
I want to make a class that can interact with a database, which has the following desired functionality:
It has a method to return all fields from the database, later can be changed such that it can also limit with it returns.
It has a method to insert a specific instance of this class.
It has a method to update a specific instance of this class.
I will show the code in a moment after further explanation.
Now I want to extract an interface, or rather an abstract class I think might be more appriopiate, to be sure that all classes/datafields follow the same 'interface', and to be able to use them as a supertype in Lists etc.
The data class, in this case Account.java, should represent a table in a database which stores {Username, Password}, I am omitting an explicite unique identifier for now, still not sure if I will make an additional ID field or use the uniqueness of the Username field.
It would be best if the abstract class itself would handle all the MySQL interaction 'mess'.
Account.java so far:
package testthing;
import java.util.Map;
/**
*
* #author Frank
*/
public class Account {
private final static String ALL_QUERY = "SELECT * FROM accounts";
private final static String INSERT_QUERY = "INSERT INTO accounts (username, password) VALUES(?, ?)";
private final static String UPDATE_QUERY = "UPDATE accounts SET password=? WHERE username=?";
private String username;
private String password;
public Account(final String username, final String password) {
this.username = username;
this.password= password;
}
public String getUsername() {
return username;
}
public void setUsername(final String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(final String password) {
this.password = password;
}
public static Map<String, Account> getAll() {
//return a map using the ALL_QUERY string
}
public void insert() {
//insert this using INSERT_QUERY
}
public void update() {
//update this using UPDATE_QUERY
}
}
I know that I haven't been that clear I'm afraid, but I hope this is enough to help me get going.
Basically I want to always be able to use the followings methods when working with a TableObject, which Account would be a subset of:
Account.getAll();
new Account("test", "test").insert();
currentAccount.setPassword("newPassword"); currentAccount.update();
All the nasty SQL stuff should be hidden inside the proposed abstract class. The only thing you cannot escape in a TableObject class is the definition of the SQL queries.
Regards.
EDIT: In current example Account.getAll() returns a Map<String, Account>, but in reality the first generic argument should be the type of the key in the database. So if you would use an unique ID then it would need to return a Map<Integer, Account>. I hope this change makes it in time for people to read it.
Is it not more logical, to have the connection code and all the "nasty" stuff in the superclass, but simply have a more general method in the superclass, that is used by it's sub classes. For example:
public void executeUpdate(String query)
{
// Code to execute update.
}
public Map<String, Data> getData(String query)
{
// Code to get data.
return data;
}
This way, these methods are more general. It means that you can implement several classes that simply pass query data, rather than constantly having to update a superclass every single time you want to add new functionality.
Obviously I've just assumed a type Data here, but that might be something to look into. The aim here is to decouple your classes as much as possible. This means you can add as many new classes as you want, and they can use their supertype without hinderence.
This also means things like
Account.getAll();
is a little less complicated, because if you have getAll in your superclass, where is the reference that you want to get all accounts? If the code is actually in the Account class, you can customize the query, and send it off to the getData method to be executed.