JDBC---实践

 

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class Test {
 
    private static class User{
        private Integer id;
        private String name;
 
        public Integer getId() {
            return id;
        }
 
        public void setId(Integer id) {
            this.id = id;
        }
 
        public String getName() {
            return name;
        }
 
        public void setName(String name) {
            this.name = name;
        }
 
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
 
    static void statementTest(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
 
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
 
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "an314159");
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select * from user where id = "+ 23 + " and name = " + "'lili' or 1=1"); // select * from user where id = 23 and name = 'lili' or 1=1
 
            User user = new User();
 
            while (resultSet.next()){
                int id = resultSet.getInt(1);
                user.setId(id);
                String name = resultSet.getString(2);
                user.setName(name);
            }
 
            System.out.println(user);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
 
            if (statement != null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
 
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
 
    static void prepareStatementTest(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
 
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
 
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "an314159");
 
            // === 查询
//            String selectSql = "select * from user where id = ? and name = ?";
//            preparedStatement = connection.prepareStatement(selectSql);
//            preparedStatement.setObject(1, 23);
//            preparedStatement.setObject(2, "'lili' or 1=1");
//            resultSet = preparedStatement.executeQuery(); // select * from user where id = 23 and name = '''lili'' or 1=1' (name的值 作为 一个字符串 处理的)
//
//            User user = new User();
//
//            while (resultSet.next()){
//                int id = resultSet.getInt(1);
//                user.setId(id);
//                String name = resultSet.getString(2);
//                user.setName(name);
//            }
//
//            System.out.println(user);
 
 
            // === 新增
            // JDBC 默认 autoCommit = true
            connection.setAutoCommit(false);
//            connection.setTransactionIsolation(1);
 
            String insertSql = "insert into user(name,sex) values(?,?)";
            preparedStatement = connection.prepareStatement(insertSql);
            preparedStatement.setObject(1,"an4");
            preparedStatement.setObject(2,"an4");
            // true: if the first result is a ResultSet object;
            // false: if the first result is an update count or there is no result
            boolean insertResult = preparedStatement.execute();
            System.out.println(insertResult);
 
            int i = 1/ 0;
 
            // 显式 commit 事务
            connection.commit();
 
 
 
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
 
            if (preparedStatement != null){
                try {
                    preparedStatement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
 
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
 
    public static void main(String[] args) {
 
//        statementTest();
 
        prepareStatementTest();
    }
 
}

  

posted on   anpeiyong  阅读(12)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-03-23 JavaSE---进制

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示