用jdbc连接数据库并简单执行SQL语句

Posted 南山南

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用jdbc连接数据库并简单执行SQL语句相关的知识,希望对你有一定的参考价值。

一:版本一.这种存在一个问题就是每执行一次操作都会创建一次Connection链接和且释放一次链接

1:创建pojo对象(OR映射,一个pojo类对应一张数据库表)

 1 package com.yinfu.dao;
 2 
 3 public class Employee {
 4 
 5     private int id;
 6     private String name;
 7     private String password;
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public String getPassword() {
21         return password;
22     }
23     public void setPassword(String password) {
24         this.password = password;
25     }
26     @Override
27     public String toString() {
28         return "Employee [id=" + id + ", name=" + name + ", password=" + password + "]";
29     }
30     public Employee(int id, String name, String password) {
31         super();
32         this.id = id;
33         this.name = name;
34         this.password = password;
35     }
36     public Employee() {
37         super();
38     }
39 }

2:创建数据库连接用的数据文件,用于外界读取数据(properties文件):

 1 driver=com.mysql.jdbc.Driver 2 jdbcUrl=jdbc:mysql://localhost:3306/test 3 user=root 4 password=song12345 

3:创建数据库连接和关闭连接的工具类(被重复使用的方法可以写在工具类中):

  1 package com.yinfu.utils;
  2 
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.sql.Connection;
  6 import java.sql.DriverManager;
  7 import java.sql.PreparedStatement;
  8 import java.sql.ResultSet;
  9 import java.sql.SQLException;
 10 import java.util.Properties;
 11 /**
 12  * JDBC的工具类,封装了jdbc的一些方法
 13  * @author lusong
 14  *
 15  */
 16 public class JDBCUtils {
 17 
 18     //关闭jdbc的链接
 19     /**
 20      * 关闭statement和connection
 21      * @param ps
 22      * @param conn
 23      */
 24     public static void release(PreparedStatement ps, Connection conn){
 25         try {
 26             if(ps != null){
 27                 ps.close();
 28             }
 29         } catch (SQLException e) {
 30             e.printStackTrace();
 31         }finally{
 32             try {
 33                 if(conn != null){
 34                     conn.close();
 35                 }
 36             } catch (SQLException e) {
 37                 e.printStackTrace();
 38             }
 39         }
 40     }
 41     public static void release(ResultSet result,PreparedStatement ps, Connection conn){
 42         try {
 43             if(result != null){
 44                 result.close();
 45             }
 46         } catch (SQLException e1) {
 47             e1.printStackTrace();
 48         }finally{
 49             try {
 50                 if(ps != null){
 51                     ps.close();
 52                 }
 53             } catch (SQLException e) {
 54                 e.printStackTrace();
 55             }finally{
 56                 try {
 57                     if(conn != null){
 58                         conn.close();
 59                     }
 60                 } catch (SQLException e) {
 61                     e.printStackTrace();
 62                 }
 63             }
 64         }
 65         
 66     }
 67     
 68     //获取jdbc的链接
 69     /**
 70      * 用于创建jdbc链接的工具类对象
 71      * @return
 72      */
 73     public static Connection getConnetions() {
 74         Connection conn = null;
 75         String driverClass = null;
 76         String jdbcUrl = null;
 77         String user = null;
 78         String password = null;
 79         
 80         try {
 81             //读取配置文件中的配置
 82             InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
 83             Properties properties = new Properties();
 84             properties.load(is);
 85             driverClass = properties.getProperty("driver");
 86             jdbcUrl = properties.getProperty("jdbcUrl");
 87             user = properties.getProperty("user");
 88             password = properties.getProperty("password");
 89             //注册驱动程序
 90             Class.forName(driverClass);
 91             //实际应该这样写(由于对应的应用程序中有一个对应的静态代码块,自动回将驱动的类对象进行驱动加载)
 92             //DriverManager.registerDriver((Driver) Class.forName(driverClass).newInstance());
 93             
 94             conn = DriverManager.getConnection(jdbcUrl,user,password);
 95             
 96         } catch (IOException e) {
 97             // TODO Auto-generated catch block
 98             e.printStackTrace();
 99         }catch (SQLException e) {
100             // TODO Auto-generated catch block
101             e.printStackTrace();
102         }catch (ClassNotFoundException e) {
103             // TODO Auto-generated catch block
104             e.printStackTrace();
105         }
106         return conn;
107     }
108 }

4:用Junit测试实现的JDBC实现数据库的增删改查操作:

 1 package com.yinfu.test;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.sql.Connection;
 6 import java.sql.DriverManager;
 7 import java.sql.PreparedStatement;
 8 import java.sql.ResultSet;
 9 import java.sql.SQLException;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Properties;
13 
14 import org.junit.Test;
15 
16 import com.yinfu.dao.Employee;
17 import com.yinfu.utils.JDBCUtils;
18 
19 public class JDBCTest {
20 
21     @Test
22     public void testUpdate(){
23         //
24         String sql = "insert into employee (Id,Name,Password) values (1,‘wangba‘,131)";
25         //26         //String sql = "delete from employee where id = 1";
27         //28         //String sql = "update employee set name = ‘fuck‘ where id = 2";
29         //
30         String sqlQuery = "select * from employee";
31         update(sql);
32         testQueryObject(sqlQuery);
33     }
34     
35     public void testQueryObject(String sql){
36         Employee employee = null;
37         List<Employee> list = new ArrayList();
38         Connection conn = null;
39         PreparedStatement ps = null;
40         ResultSet result = null;
41         try {
42             //创建连接
43             conn = JDBCUtils.getConnetions();
44             //创建prepareStatement对象,用于执行SQL
45             ps = conn.prepareStatement(sql);
46             //获取查询结果集
47             result = ps.executeQuery();
48             while(result.next()){
49                 employee = new Employee(result.getInt(1),result.getString(2),result.getString(3));
50                 list.add(employee);
51             }
52             System.out.println(list);
53         } catch (Exception e) {
54             e.printStackTrace();
55         }finally{
56             JDBCUtils.release(result, ps, conn);
57         }
58     }
59     
60     public void update(String sql){
61         Connection conn = null;
62         PreparedStatement ps = null;
63         try {
64             //创建数据库连接
65             conn = JDBCUtils.getConnetions();
66             //创建执行SQL的prepareStatement对象
67             ps = conn.prepareStatement(sql);
68             //用于增删改操作
69             int result = ps.executeUpdate();
70             System.out.println(result);
71         } catch (Exception e) {
72             System.out.println("出现异常1="+e.toString());
73         }finally{
74             JDBCUtils.release(ps, conn);
75         }
76 
77         
78     }
79 }

 

以上是关于用jdbc连接数据库并简单执行SQL语句的主要内容,如果未能解决你的问题,请参考以下文章

JDBC连接数据库并执行语句入门案例

JDBC的一些简单通用代码

JDBC实现简单的增删改查

eclipse用jdbc连接SQL server时出现了问题……这是啥情况啊!请大神指教!

JDBC# JDBC的简单理解概述

JDBC编程的方式