JDBC基本开发

JDBC基本开发步骤

一:注册驱动

1 方式一:DriverManager.registerDriver(new Driver()); //存在注册两次问题,性能较低,消耗资源
2 方式二:Class.forName("com.mysql.jdbc.Driver"); //开发中推荐

二:获取连接对象

//导入Java.sql包下COnnection
Connnection conn = DriverManager.getConnection();

三: 创建执行SQL语句的Statement对象

方式一:Statement stmt = conn.createStatement(); //存在SQL注入的问题,不推荐使用
方式二: Preparedstatement ps = conn.prepareStatement(); // 预编译对象,解决SQL注入问题

四:执行SQL语句

  查询

ResultSet rs = ps.executeQuery(sql语句)
  //判断结果是否存在结果  next()
  while(rs.next()) {
        //获取结果
        getXxx(列的值)
        getString(列的名)
  }

  增删改

executeUpdate(sql语句)
execute(sql) 结果是布尔类型,如果是查询操作返回true,增删改false

五:释放资源

查询: 关闭结果集  statement对象  connection对象
增删改: statement对象  connection对象

 

 

JDBC工具类编写

第一步:将工具类构造方法私有化

第二步:在工具类里面对外的访问方法,方法修饰为静态static的

 1 public class JdbcUtils {
 2     
 3     //工具类的构造方法私有化
 4     private JdbcUtils() {
 5         
 6     }
 7     
 8     //提供一个获取连接对象的方法
 9     public static Connection getConnection() throws Exception {
10         //1,注册驱动
11         //DriverManager.registerDriver(new Driver());  //硬编码
12         Class.forName("com.mysql.jdbc.Driver");
13         //2,获取连接对象
14         String url = "jdbc:mysql://localhost:3306/heimatmall";
15         String user = "root";
16         String password = "123456";
17         Connection conn = DriverManager.getConnection(url, user, password);
18         return conn;
19     }
20     
21     //针对查询释放资源
22     public static void release(ResultSet rs,Statement stmt,Connection conn) {
23         
24         if(rs != null) { //判断对象不为空
25             try {
26                 rs.close();
27             } catch (SQLException e) {
28                 // TODO Auto-generated catch block
29                 e.printStackTrace();
30             }
31             rs = null; //Java自动垃圾机制,加快垃圾对象回收
32         }
33         
34         if(stmt != null) {
35             try {
36                 stmt.close();
37             } catch (SQLException e) {
38                 // TODO Auto-generated catch block
39                 e.printStackTrace();
40             }
41             stmt = null;
42         }
43         
44         if(conn != null) {
45             try {
46                 conn.close();
47             } catch (SQLException e) {
48                 // TODO Auto-generated catch block
49                 e.printStackTrace();
50             }
51             conn = null;
52         }
53         
54     }
55     
56     
57     //针对增删改释放资源
58     public static void release(Statement stmt,Connection conn) {
59         
60         if(stmt != null) {
61             try {
62                 stmt.close();
63             } catch (SQLException e) {
64                 // TODO Auto-generated catch block
65                 e.printStackTrace();
66             }
67             stmt = null;
68         }
69         
70         if(conn != null) {
71             try {
72                 conn.close();
73             } catch (SQLException e) {
74                 // TODO Auto-generated catch block
75                 e.printStackTrace();
76             }
77             conn = null;
78         }
79         
80     }
81     
82 }

 

读取配置文件

方式一:使用Properties读取配置文件

​ 步骤一:创建properties对象

​ 步骤二:创建字节流对象,读取配置文件

​ 步骤三: 将字节流对象传给properites对象。使用load方法关联

​ 步骤四: 使用getProperty(“键的名字”)获取指定的值

  1 public class MyJdbcUtils {
  2     private static String driverName;
  3     private static String url;
  4     private static String user;
  5     private static String password;
  6     
  7     //工具类的构造方法私有化
  8     private MyJdbcUtils() {
  9         
 10     }
 11     
 12     //静态代码块:随着类的加载而加载,并且只执行一次
 13     static {
 14         //注册驱动
 15         try {
 16             //System.out.println("开始执行静态代码块里面的代码....读取配置文件完成注册驱动");
 17             //第一步:创建Properties对象
 18             Properties prop = new Properties();
 19             //第二步:创建一个 字节流对象,关联要读取的properties文件
 20             FileInputStream fis = new FileInputStream("src/jdbc.properties");
 21             //第三步:传入字节流对象给properties对象
 22             prop.load(fis);
 23             //第四步:获取数据
 24             driverName = prop.getProperty("DriverName");
 25             url = prop.getProperty("url");
 26             user = prop.getProperty("user");
 27             password = prop.getProperty("password");
 28             //注册驱动
 29             Class.forName(driverName);
 30         } catch (Exception e) { //异常处理快捷键:alt++shift+z
 31             // TODO Auto-generated catch block
 32             e.printStackTrace();
 33         }
 34     }
 35     
 36     //提供一个获取连接对象的方法
 37     public static Connection getConnection() throws Exception {
 38         Connection conn = DriverManager.getConnection(url, user, password);
 39         return conn;
 40     }
 41     
 42     //针对查询释放资源
 43     public static void release(ResultSet rs,Statement stmt,Connection conn) {
 44         
 45         if(rs != null) { //判断对象不为空
 46             try {
 47                 rs.close();
 48             } catch (SQLException e) {
 49                 // TODO Auto-generated catch block
 50                 e.printStackTrace();
 51             }
 52             rs = null; //Java自动垃圾机制,加快垃圾对象回收
 53         }
 54         
 55         if(stmt != null) {
 56             try {
 57                 stmt.close();
 58             } catch (SQLException e) {
 59                 // TODO Auto-generated catch block
 60                 e.printStackTrace();
 61             }
 62             stmt = null;
 63         }
 64         
 65         if(conn != null) {
 66             try {
 67                 conn.close();
 68             } catch (SQLException e) {
 69                 // TODO Auto-generated catch block
 70                 e.printStackTrace();
 71             }
 72             conn = null;
 73         }
 74         
 75     }
 76     
 77     
 78     //针对增删改释放资源
 79     public static void release(Statement stmt,Connection conn) {
 80         
 81         if(stmt != null) {
 82             try {
 83                 stmt.close();
 84             } catch (SQLException e) {
 85                 // TODO Auto-generated catch block
 86                 e.printStackTrace();
 87             }
 88             stmt = null;
 89         }
 90         
 91         if(conn != null) {
 92             try {
 93                 conn.close();
 94             } catch (SQLException e) {
 95                 // TODO Auto-generated catch block
 96                 e.printStackTrace();
 97             }
 98             conn = null;
 99         }
100         
101     }
102     
103 }

方式二:使用ResourceBundle对象读取

1 //扩展:使用ResourceBundle读取,注意:读取的目录是src下面的properties文件
2 ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
3             driverName = bundle.getString("DriverName");
4             url = bundle.getString("url");
5             user = bundle.getString("user");
6             password = bundle.getString("password");
7             Class.forName(driverName);

 

posted @ 2017-10-23 12:33  Dear丶配角  阅读(159)  评论(0编辑  收藏  举报