当前位置:Gxlcms > 数据库问题 > 由浅到深学习JDBC三

由浅到深学习JDBC三

时间:2021-07-01 10:21:17 帮助过:24人阅读

 class JDBC_Util3 {

       private static final Properties prop = new Properties();

       private static final ThreadLocal<Connection> tdl = new ThreadLocal<Connection>();

       

       static{

             InputStream is = null;

             try {

                    is = JDBC_Util3.class.getResourceAsStream("jdbc.properties");

                    prop.load(is);

                    String driverName = prop.getProperty("driverName");

                    Class.forName(driverName);

             } catch (Exception e) {

                    e.printStackTrace();

             }

             

       }

       //返回链接

       public static Connection getConnection() throws Exception {

             Connection conn = null;

             conn = tdl.get();//获得当前线程连接

             if(conn == null){

                    //说明当前线程没有conn

                    String user = prop.getProperty("user");

                    String password = prop.getProperty("password");

                    String url = prop.getProperty("url");

                    conn = DriverManager.getConnection(url,user,password);

                    //将当 conn存入线程

                    tdl.set(conn);

             }

             return conn;

       }

       /**

        * 释放资源

        */

       public static void release(ResultSet rs,Statement stm,Connection conn){

             try {

                    if(rs!=null){rs.close();}

                    if(stm!=null){stm.close();}

                    if(conn!=null){

                           conn.close();

                           tdl.remove();//移除当前线程对象中的conn

                           }

             } catch (SQLException e) {

                    e.printStackTrace();

             }

       }

}

JDBC_Dao3.java

public class JDBC_Dao3 {

       / /更新账户

       public void upDateAccount(Account toAcc) {

             Connection conn = null;

             PreparedStatement pstm = null;

             try {

                    conn = JDBC_Util3.getConnection();

                    String sql = "update account set card_id=?,"

                                 + "password=?,balance=?,phone=? where card_id=?";

                    pstm = conn.prepareStatement(sql);

                    pstm.setString(1, toAcc.getCardId());

                    pstm.setString(2, toAcc.getPassword());

                    pstm.setDouble(3, toAcc.getBalance());

                    pstm.setString(4, toAcc.getPhone());

                    pstm.setString(5, toAcc.getCardId());

                    int i  = pstm.executeUpdate();

             } catch (Exception e) {

                    e.printStackTrace();

             }finally{

                    JDBC_Util.release(null, pstm, null);

             }

       }

        // 查询账户

       public Account queryAccount(Integer toCardId) {

             Connection conn = null;

             Statement stm = null;

             ResultSet rs = null;

             Account acc = null;

             try {

                    conn = JDBC_Util3.getConnection();

                    stm = conn.createStatement();

                    String sql = "select * from account where card_id = "

                    + toCardId;

                    System.out.println(sql);

                    rs = stm.executeQuery(sql);

                    while (rs.next()) {

                           acc = new Account();

                           acc.setCardId(rs.getString("card_id"));

                           acc.setPassword(rs.getString("password"));

                           acc.setBalance(rs.getDouble("balance"));

                           acc.setPhone(rs.getString("phone"));

                    }

             } catch (Exception e) {

             }finally{

                    JDBC_Util.release(rs, stm, null);

             }

             return acc;

       }

}

JDBC_Service.java

public class JDBC_Service {  

       public void transfer(Integer fromCardId,String password,

                    Integer toCardId,Double money){

             Connection conn = null;

             try{

                    conn

人气教程排行