java jdbc数据库操作
时间:2021-07-01 10:21:17
帮助过:2人阅读
package shb.java.demo3;
2
3 import java.sql.DriverManager;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.sql.Statement;
7 /**
8 * jdbc操作数据库
9 * @Package:com.cnpc.dlp.web.event.controller
10 * @Description:
11 * @author Shihaobin
12 * @Date 2015-8-27下午1:31:15
13 */
14 public class Demo12 {
15 public static void main(String[] args) {
16 String url = "jdbc:mysql://localhost:3306/base"
;
17 String username = "root"
;
18 String password = "bin"
;
19 try {
20 //加载数据库驱动
21 Class.forName("com.mysql.jdbc.Driver"
);
22 }
catch (ClassNotFoundException e) {
23 // TODO Auto-generated catch block
24 e.printStackTrace();
25 }
26 try {
27 //获得和数据库连接
28 java.sql.Connection connection=
DriverManager.getConnection(url, username, password);
29 //创建查询对象
30 Statement statement =
connection.createStatement();
31 String sqlinsert = "insert into usertable (username,password)values(‘lizhe‘,‘923840298‘)"
;
32 String sqlString = "select username from usertable u where u.password = ‘sdf‘"
;
33 //执行sql语句
34 statement.execute(sqlinsert);
35 System.out.println("success"
);
36 statement.close();
37 connection.close();
38 }
catch (SQLException e) {
39 // TODO Auto-generated catch block
40 e.printStackTrace();
41 }
42
43 }
44 }
如有错误请各位纠正,谢谢。
java jdbc数据库操作
标签: