使用原生的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 }

猜你喜欢

转载自www.cnblogs.com/L-o-g-i-c/p/11719121.html