码迷,mamicode.com
首页 > 数据库 > 详细

JDBC编程之优化

时间:2016-08-04 23:19:35      阅读:425      评论:0      收藏:0      [点我收藏+]

标签:

1、创建 dbconfig.properties

driver=com.mysql.jdbc.Driver
dburl=jdbc\:mysql\://localhost\:3306/mytest
user=root
password=


2、创建 ConnectionFactory.java

 1 package com.test.util;
 2 
 3 import java.io.InputStream;
 4 import java.sql.Connection;
 5 import java.sql.DriverManager;
 6 import java.util.Properties;
 7 
 8 public class ConnectionFactory {
 9 
10     private static String driver;
11     private static String dburl;
12     private static String user;
13     private static String password;
14     
15     private static final ConnectionFactory factory = new ConnectionFactory();
16     
17     private Connection conn;
18     
19     static{
20         Properties prop = new Properties();
21         try{
22             InputStream in = ConnectionFactory.class.getClassLoader()
23                     .getResourceAsStream("dbconfig.properties");
24             prop.load(in);
25         }catch(Exception e){
26             System.out.println("配置文件读取错误");
27         }
28         driver = prop.getProperty("driver");
29         dburl = prop.getProperty("dburl");
30         user = prop.getProperty("user");
31         password = prop.getProperty("password");
32         
33     }
34     private ConnectionFactory(){
35         
36     }
37     public static ConnectionFactory getInstance(){
38         return factory;
39     }
40     public Connection makeConnection(){
41         try{
42             Class.forName(driver);
43             conn = DriverManager.getConnection(dburl,user,password);
44         }catch(Exception e){
45             e.printStackTrace();
46         }
47         return conn;
48     }
49 }

 

3、抽象类 IdEntity.java

 1 package com.test.entity;
 2 
 3 public abstract class IdEntity {
 4  
 5     protected int id;
 6     public int getId(){
 7         return id;
 8     }
 9     public void setId(int id){
10         this.id = id;
11     }
12 }

 

4、User.java 继承抽象类IdEntity.java

 1 package com.test.entity;
 2 
 3 public class User extends IdEntity {
 4     private String name;
 5     private String password;
 6     
 7     public String getName() {
 8         return name;
 9     }
10     public void setName(String name) {
11         this.name = name;
12     }
13     public String getPassword() {
14         return password;
15     }
16     public void setPassword(String password) {
17         this.password = password;
18     }
19     @Override
20     public String toString() {
21         return "User [name=" + name + ", password=" + password + ", id=" + id
22                 + "]";
23     }
24     
25     
26     
27 }


5、Address.java 继承抽象类IdEntity.java

 1 package com.test.entity;
 2 
 3 public class Address extends IdEntity{
 4 
 5     private String address;
 6     private int userid;
 7     public String getAddress() {
 8         return address;
 9     }
10     public void setAddress(String address) {
11         this.address = address;
12     }
13     public int getUserid() {
14         return userid;
15     }
16     public void setUserid(int userid) {
17         this.userid = userid;
18     }
19     @Override
20     public String toString() {
21         return "Address [address=" + address + ", userid=" + userid + ", id="
22                 + id + "]";
23     }
24     
25     
26 }


6、UserDao.java 定义接口

 1 package com.test.dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5 
 6 import com.test.entity.User;
 7 
 8 public interface UserDao {
 9 
10     public void save(Connection conn,User user)throws SQLException;
11     
12     public void update(Connection conn,int id,User user) throws SQLException;
13     
14     public void delete(Connection conn,User user)throws SQLException;
15     
16 }


7、实现UserDao接口,UserDaoImpl.java

 1 package com.test.dao.impl;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.SQLException;
 6 
 7 import com.test.dao.UserDao;
 8 import com.test.entity.User;
 9 
10 public class UserDaoImpl implements UserDao{
11 
12     @Override
13     public void save(Connection conn, User user) throws SQLException {
14         PreparedStatement ps = conn.prepareCall("insert into person(name,password) values(?,?)");
15         ps.setString(1, user.getName());
16         ps.setString(2, user.getPassword());
17         ps.execute();
18     }
19 
20     @Override
21     public void update(Connection conn, int id, User user) throws SQLException {
22         String updateSql ="update person set name= ?,password=? where id =?";
23         PreparedStatement ps = conn.prepareStatement(updateSql);
24         ps.setString(1, user.getName());
25         ps.setString(2, user.getPassword());
26         ps.setInt(3, id);
27         ps.execute();
28     }
29 
30     @Override
31     public void delete(Connection conn, User user) throws SQLException {
32         PreparedStatement ps = conn.prepareStatement("delete from uesr where id = ?");
33         ps.setInt(1, user.getId());
34         ps.execute();
35     }
36 
37 }

 

8、测试类 UserDaoTest.java

 1 package com.test.testclass;
 2 
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5 
 6 import com.test.dao.UserDao;
 7 import com.test.dao.impl.UserDaoImpl;
 8 import com.test.entity.User;
 9 import com.test.util.ConnectionFactory;
10 
11 public class UserDaoTest {
12 
13     public static void main(String[] args) {
14         Connection conn = null;
15         
16         
17         try {
18             conn = ConnectionFactory.getInstance().makeConnection();
19             conn.setAutoCommit(false);
20             UserDao userDao = new UserDaoImpl();
21             User tom = new User();
22             tom.setName("tom");
23             tom.setPassword("123456");
24             userDao.save(conn, tom);
25             conn.commit();
26         } catch (SQLException e) {
27             try{
28                 conn.rollback();
29             }catch(Exception e2){
30                 e2.printStackTrace();
31             }
32             e.printStackTrace();
33         }
34         
35     }
36 }

 

JDBC编程之优化

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/5738610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!