Skip to content

Commit 4196075

Browse files
author
nononi
committed
第八周作业2
1 parent 4f93a25 commit 4196075

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package io.nononi.db.dbdemo.shardingdb;
2+
3+
import java.sql.*;
4+
import java.util.Random;
5+
6+
/**
7+
* sharding_db demo
8+
*/
9+
public class ShardingDbDemo {
10+
public static void main(String[] args) throws Exception {
11+
Connection conn = null;
12+
Random random = new Random();
13+
try {
14+
conn = getConn();
15+
conn.setAutoCommit(false);
16+
System.out.println("开始查询:");
17+
select(conn);
18+
insert(conn, random.nextInt(100000), "OK");
19+
System.out.println("插入后查询:");
20+
select(conn);
21+
update(conn, 724643077406830593L, "FAIL");
22+
System.out.println("更新后查询:");
23+
select(conn);
24+
delete(conn, 724643077406830593L);
25+
System.out.println("删除后查询:");
26+
select(conn);
27+
conn.commit();
28+
29+
} catch (SQLException e) {
30+
e.printStackTrace();
31+
try {
32+
conn.rollback();
33+
} catch (SQLException ex) {
34+
ex.printStackTrace();
35+
}
36+
} finally {
37+
conn.close();
38+
}
39+
40+
}
41+
42+
private static Connection getConn() {
43+
String driver = "com.mysql.jdbc.Driver";
44+
String url = "jdbc:mysql://localhost:3307/sharding_db";
45+
String username = "root";
46+
String password = "root";
47+
Connection conn = null;
48+
try {
49+
Class.forName(driver); //classLoader,加载对应驱动
50+
conn = (Connection) DriverManager.getConnection(url, username, password);
51+
} catch (ClassNotFoundException e) {
52+
e.printStackTrace();
53+
} catch (SQLException e) {
54+
e.printStackTrace();
55+
}
56+
return conn;
57+
}
58+
59+
private static void select(Connection connection) throws SQLException {
60+
String sql = "SELECT * FROM `t_order`;";
61+
PreparedStatement stmt = connection.prepareStatement(sql);
62+
ResultSet resultSet = stmt.executeQuery();
63+
while (resultSet.next()) {
64+
System.out.print(resultSet.getLong("order_id") + " ");
65+
System.out.print(resultSet.getInt("user_id") + " ");
66+
System.out.println(resultSet.getString("status") + " ");
67+
}
68+
System.out.println("");
69+
}
70+
71+
private static int insert(Connection connection, int user_id, String status) throws SQLException {
72+
String sql = "INSERT INTO `t_order` (user_id, status) VALUES(?, ?);";
73+
PreparedStatement stmt = connection.prepareStatement(sql);
74+
stmt.setInt(1,user_id);
75+
stmt.setString(2,status);
76+
return stmt.executeUpdate();
77+
78+
}
79+
80+
private static int update(Connection connection, long order_id, String status) throws SQLException {
81+
String sql = "UPDATE `t_order` SET status = ? WHERE order_id = ?;";
82+
PreparedStatement stmt = connection.prepareStatement(sql);
83+
stmt.setString(1,status);
84+
stmt.setLong(2,order_id);
85+
return stmt.executeUpdate();
86+
}
87+
88+
private static int delete(Connection connection, long order_id) throws SQLException {
89+
90+
91+
String sql = "DELETE FROM `t_order` WHERE order_id=? ;";
92+
PreparedStatement stmt = connection.prepareStatement(sql);
93+
stmt.setLong(1,order_id);
94+
return stmt.executeUpdate();
95+
}
96+
97+
}

0 commit comments

Comments
 (0)