使用原生的jdbc連接數據庫進行查詢


  1. 創建數據庫和數據庫表

    

##-- 1.創建數據庫
create database test;

##-- 2.創建表
DROP TABLE IF EXISTS tb_user;
CREATE TABLE tb_user (
       id int(32) NOT NULL,
       user_name varchar(32) DEFAULT NULL,
       password varchar(32) DEFAULT NULL,
       name varchar(32) DEFAULT NULL,
       age int(10) DEFAULT NULL,
       sex int(2) DEFAULT NULL,
       birthday date DEFAULT NULL,
       created datetime DEFAULT NULL,
       updated datetime DEFAULT NULL,
       PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

##-- 3.插入數據
INSERT INTO tb_user.tb_user ( user_name, password, name, age, sex, birthday, created, updated) VALUES ( 'zpc', '123456', '鵬程', '22', '1', '1990-09-02', now(), now());
INSERT INTO tb_user.tb_user ( user_name, password, name, age, sex, birthday, created, updated) VALUES ( 'hj', '123456', '靜靜', '22', '1', '1993-09-05', now(), now());

  2. 創建一個maven項目,加入jar包 mysql-connector-java

    pom.xml如下:

    

1 <dependency>
2     <groupId>mysql</groupId>
3     <artifactId>mysql-connector-java</artifactId>
4     <version>5.1.32</version>
5 </dependency>

  3.java代碼:

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         insert();
 5     }
 6 
 7     private static Connection createConnection(){
 8         try {
 9             Connection connection = null;
10             //加載驅動
11             Class.forName("com.mysql.jdbc.Driver");
12             String url = "jdbc:mysql://127.0.0.1:3306/test";
13             String user = "root";
14             String password = "root";
15             connection = DriverManager.getConnection(url, user, password);
16             return connection;
17         } catch (ClassNotFoundException e) {
18             e.printStackTrace();
19         } catch (SQLException e) {
20             e.printStackTrace();
21         }
22         return null;
23     }
24 
25 
26     public static void insert(){
27         try {
28             String sql = "INSERT INTO test.tb_user (user_name, password, name, age, sex, birthday, created, updated) VALUES ( 'zpc', '123456', '鵬程', '22', '1', '1990-09-02', ?, ?);";
29             PreparedStatement ps = createConnection().prepareStatement(sql);
30             //獲取ParameterMetaData對象
31             ParameterMetaData data = ps.getParameterMetaData();
32             //獲取參數個數
33             int parameterCount = data.getParameterCount();
34             //設置參數數組
35             for (int i = 1; i <= parameterCount; i++) {
36                 ps.setObject(i,new Date());
37             }
38             int i = ps.executeUpdate();
39             System.out.println(i);
40         } catch (SQLException e) {
41             e.printStackTrace();
42         }
43     }
44 
45 
46     public static void update(){
47         try {
48             String sql = " update tb_user set user_name = ? where id = ? ";
49             PreparedStatement ps = createConnection().prepareStatement(sql);
50             //獲取ParameterMetaData對象
51             ParameterMetaData data = ps.getParameterMetaData();
52             //獲取參數個數
53             int parameterCount = data.getParameterCount();
54             //設置參數數組
55             String[] arr = {"hj","1"};
56             for (int i = 1; i <= parameterCount; i++) {
57                 ps.setObject(i,arr[i-1]);
58             }
59             int i = ps.executeUpdate();
60             System.out.println(i);
61         } catch (SQLException e) {
62             e.printStackTrace();
63         }
64     }
65 
66     public static void select(){
67         PreparedStatement preparedStatement = null;
68         ResultSet resultSet = null;
69         try {
70             String sql = "select * from tb_user where id=?";
71             preparedStatement = createConnection().prepareStatement(sql);
72             // 設置參數
73             preparedStatement.setLong(1, 1);
74             resultSet = preparedStatement.executeQuery();
75             // 處理結果集
76             while (resultSet.next()) {
77                 System.out.println(resultSet.getString("user_name"));
78                 System.out.println(resultSet.getString("name"));
79                 System.out.println(resultSet.getInt("age"));
80                 System.out.println(resultSet.getDate("birthday"));
81             }
82         } catch (SQLException e) {
83             e.printStackTrace();
84         }finally {
85             try {
86                 if(resultSet != null)resultSet.close();
87                 if (preparedStatement != null)preparedStatement.close();
88             } catch (SQLException e) {
89                 e.printStackTrace();
90             }
91         }
92     }
93 
94 
95 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM