全面了解JAVA_BaseDAO数据处理类

时间:2022-04-21 06:57:13

实例如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package dao;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
 
public class BaseDao {
  private String drive="com.microsoft.sqlserver.jdbc.SQLServerDriver";
  private String url="jdbc:sqlserver://localhost:1433;DatabaseName=BookManage";
  private String user="sa";
  private String password="";
  protected Connection conn;
  protected PreparedStatement ps;
  protected ResultSet rs;
  public Connection getCon(){
    try {
      Class.forName(drive);
      conn=DriverManager.getConnection(url, user, password);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    
    return conn;
  }
  public void allClos(ResultSet rs,PreparedStatement ps,Connection conn){
  
      try {
        if(rs!=null){
        rs.close();
        }
        if(ps!=null){
          ps.close();
          }
        if(conn!=null){
          conn.close();
          }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
public int executeUpdate(String sql,Object...str){
  int num=0;
  try {
    ps=getCon().prepareStatement(sql);
    if(ps!=null){
      for(int i=0;i<str.length;i++){
        ps.setObject(i+1, str[i]);      }
    }
    num=ps.executeUpdate();
  } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }finally
  {
    allClos(rs, ps, conn);
  }
  return num;
}
 
}

以上这篇全面了解JAVA_BaseDAO数据处理类就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。