博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JDBC编程之优化
阅读量:4981 次
发布时间:2019-06-12

本文共 5857 字,大约阅读时间需要 19 分钟。

1、创建 dbconfig.properties

driver=com.mysql.jdbc.Driverdburl=jdbc\:mysql\://localhost\:3306/mytestuser=rootpassword=

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     @Override20     public String toString() {21         return "User [name=" + name + ", password=" + password + ", id=" + id22                 + "]";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     @Override20     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     @Override13     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     @Override21     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     @Override31     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 }

 

转载于:https://www.cnblogs.com/UniqueColor/p/5738610.html

你可能感兴趣的文章
JSP常用标签
查看>>
九涯的第一次
查看>>
处理器管理与进程调度
查看>>
向量非零元素个数_向量范数详解+代码实现
查看>>
java if 用法详解_Java编程中的条件判断之if语句的用法详解
查看>>
matlab sin函数 fft,matlab的fft函数的使用教程
查看>>
mysql sin() 函数
查看>>
单片机复位电路
查看>>
php json_decode失败,返回null
查看>>
3-day3-list-truple-map.py
查看>>
Edit控件显示多行文字
查看>>
JS第二周
查看>>
dataTable.NET的search box每輸入一個字母進行一次檢索的問題
查看>>
Python 文件处理
查看>>
邻接表详解
查看>>
迭代dict的value
查看>>
eclipse package,source folder,folder区别及相互转换
查看>>
Py 可能是最全面的 python 字符串拼接总结(带注释版)
查看>>
《Java程序设计实验》 软件工程18-1,3 OO实验2
查看>>
【Herding HDU - 4709 】【数学(利用叉乘计算三角形面积)】
查看>>