JDBC实现数据库表格到java对象的转换。

  在Java领域,当人们谈到对象持久化的时候,大多人都能想到hibernate。使用hibernate能够简化Java开发,并且使Java代码具有良好的移植性。不管换成什么样的数据库,上层的代码都一样,只要在hibernate配置文件里,加上一点点修改就能完成任务,它平衡了各大数据库的差异,以统一的接口实现了数据库的通常操作。
  hibernate到底带给了我们什么呢?数据库里的表格可以当做Java类看,而里面的记录则可以看成Java对象。而hibernate最主要要做的事情就是如何将记录映射成Java类对象。hibernate还提供了数据库连接池和对操作的数据库对象管理的方案,提交客户响应速度。
  下面将给出一个例子,使用jdbc映射数据库,从而揭露hibernate的原理。
简单领域对象User.java
package com.model;
public class User {
private Integer id;
private String username;
private String password;

public User() {
super();
}

public User(Integer id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}

操作接口UserManager.java
package com.manager;
import java.util.List;
import com.model.User;

public interface UserManager {
User get(Integer id);
User get(String username,String password);
List<User> getAll();
void save(User user);
void update(User user);
}

接口的实现 UserDao.java
package com.manager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.model.User;

public class UserDao implements UserManager {
private String driverName;
private String url;
private String username;
private String password;

public UserDao() {
super();
}

public UserDao(String driverName, String url, String username,
String password) {
super();
this.driverName = driverName;
this.url = url;
this.username = username;
this.password = password;
}

public String getDriverName() {
return driverName;
}

public void setDriverName(String driverName) {
this.driverName = driverName;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

private Connection getConnection() {
try {
Class.forName(driverName);
return DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

@Override
public User get(Integer id) {
// TODO Auto-generated method stub
String queryString = "select * from user where id=" + id;
Connection conn;
User user = new User();
if ((conn = getConnection()) == null)
return null;
try {
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery(queryString);
while (result.next()) {
Integer user_id = result.getInt("id");
String user_username = result.getString("username");
String user_password = result.getString("password");
user.setId(user_id);
user.setUsername(user_username);
user.setPassword(user_password);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

return user;
}

@Override
public User get(String username, String password) {
String queryString = "select * from user where username=" + username
+ " and password=" + password;
Connection conn;
User user = new User();
if ((conn = getConnection()) == null)
return null;
try {
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery(queryString);
while (result.next()) {
Integer user_id = result.getInt("id");
String user_username = result.getString("username");
String user_password = result.getString("password");
user.setId(user_id);
user.setUsername(user_username);
user.setPassword(user_password);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

return user;
}

@Override
public List<User> getAll() {
// TODO Auto-generated method stub
String queryString = "select * from user";
Connection conn;
List<User> list = new ArrayList<User>();
if ((conn = getConnection()) == null)
return null;
try {
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery(queryString);
while (result.next()) {
User user = new User();
Integer user_id = result.getInt("id");
String user_username = result.getString("username");
String user_password = result.getString("password");
user.setId(user_id);
user.setUsername(user_username);
user.setPassword(user_password);
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return list;
}

return list;
}

@Override
public void save(User user) {
// TODO Auto-generated method stub
String queryString = "insert into user (username,password) values ('"
+ user.getUsername() + "','" + user.getPassword() + "')";
Connection conn;
List<User> list = new ArrayList<User>();
if ((conn = getConnection()) == null)
return;

Statement stmt;
try {
stmt = conn.createStatement();
stmt.execute(queryString);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

@Override
public void update(User user) {
// TODO Auto-generated method stub
String queryString = "update  user set username='"+user.getUsername()+"'," +
"password'"+user.getPassword()+"'wherer id="+user.getId();
Connection conn;
List<User> list = new ArrayList<User>();
if ((conn = getConnection()) == null)
return;

Statement stmt;
try {
stmt = conn.createStatement();
stmt.execute(queryString);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

测试类
package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.manager.UserDao;
import com.manager.UserManager;
import com.model.User;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String dirverName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/sample";
        String username="root";
        String password="lilonglai";
        UserManager userManager = new UserDao(dirverName,url,username,password);
        List list=userManager.getAll();
        int i=0;
        while(i<list.size()){
        User user=(User)list.get(i);
        System.out.print("id:"+user.getId());
        System.out.print("  username:"+user.getUsername());
        System.out.println("   password:"+user.getPassword());
        i++;
        }
       
}
}


这样就完成了从数据库到Java类的转换,这大概也是hibernate的操作方法。

你可能感兴趣的:(java,sql,Hibernate,mysql,jdbc)