package com.xyyz.jdbc;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7
8 public class JDBCDemo {
9
10 public static void main(String[] args)
throws Exception {
11 query();
12 insert();
13 query();
14 update();
15 query();
16 delete();
17 query();
18 }
19
20 /**
21 * 查询代码
22 *
23 * @throws Exception
24 */
25 public static void query()
throws Exception {
26 // 注册驱动
27 Class.forName("com.mysql.jdbc.Driver"
);
28 // 连接数据需要的参数
29 String url = "jdbc:mysql:///jdbctestdata"
;
30 String user = "root"
;
31 String password = "root"
;
32 // 获取数据库连接
33 Connection connection =
DriverManager.getConnection(url, user, password);
34 String sql = "select * from jdbctestdata"
;
35 // 获取预编译对象
36 PreparedStatement prepareStatement =
connection.prepareStatement(sql);
37 // 执行sql语句
38 ResultSet resultSet =
prepareStatement.executeQuery();
39 // 遍历打印sql语句
40 while (resultSet.next()) {
41 System.out.println(resultSet.getInt(1) + "--" + resultSet.getString(2
));
42 }
43 // 关闭释放资源
44 resultSet.close();
45 prepareStatement.close();
46 connection.close();
47 }
48
49 /**
50 * 插入数据
51 */
52 public static void insert()
throws Exception {
53 // 注册驱动
54 Class.forName("com.mysql.jdbc.Driver"
);
55 // 连接数据需要的参数
56 String url = "jdbc:mysql:///jdbctestdata"
;
57 String user = "root"
;
58 String password = "root"
;
59 // 获取数据库连接
60 Connection connection =
DriverManager.getConnection(url, user, password);
61 String sql = "insert into jdbctestdata value(?,?,?)"
;
62 // 获取预编译对象
63 PreparedStatement prepare =
connection.prepareStatement(sql);
64 prepare.setString(1, "6"
);
65 prepare.setString(2, "小白"
);
66 prepare.setString(3, "30"
);
67 int i =
prepare.executeUpdate();
68 System.out.println("i=" +
i);
69 // 执行sql语句
70 prepare.close();
71 connection.close();
72 }
73
74 /**
75 * 更新数据
76 */
77 public static void update()
throws Exception {
78 // 注册驱动
79 Class.forName("com.mysql.jdbc.Driver"
);
80 // 连接数据需要的参数
81 String url = "jdbc:mysql:///jdbctestdata"
;
82 String user = "root"
;
83 String password = "root"
;
84 // 获取数据库连接
85 Connection connection =
DriverManager.getConnection(url, user, password);
86 String sql = "update jdbctestdata set name=? , age=? where id=?"
;
87 // 获取预编译对象
88 PreparedStatement prepare =
connection.prepareStatement(sql);
89 prepare.setString(1, "小红"
);
90 prepare.setString(2, "20"
);
91 prepare.setString(3, "6"
);
92 int i =
prepare.executeUpdate();
93 System.out.println("i=" +
i);
94 // 执行sql语句
95 prepare.close();
96 connection.close();
97 }
98
99 /**
100 * 删除数据
101 */
102 public static void delete()
throws Exception {
103 // 注册驱动
104 Class.forName("com.mysql.jdbc.Driver"
);
105 // 连接数据需要的参数
106 String url = "jdbc:mysql:///jdbctestdata"
;
107 String user = "root"
;
108 String password = "root"
;
109 // 获取数据库连接
110 Connection connection =
DriverManager.getConnection(url, user, password);
111 String sql = "delete from jdbctestdata where id = ?"
;
112 // 获取预编译对象
113 PreparedStatement prepare =
connection.prepareStatement(sql);
114 prepare.setString(1, "6"
);
115 int i =
prepare.executeUpdate();
116 System.out.println("i=" +
i);
117 // 执行sql语句
118 prepare.close();
119 connection.close();
120 }
121
122 }
http://download.csdn.net/detail/u010798073/9888901
http://download.csdn.net/detail/u010798073/9888913
简单的JDBC的增删改查操作,附源码
标签:遍历 更新 sdn user http statement 对象 插入数据 next