|
1 | 1 | package com.code.repository.study.jdbc; |
2 | 2 |
|
| 3 | +import com.mysql.jdbc.Driver; |
| 4 | + |
3 | 5 | import java.sql.Connection; |
4 | 6 | import java.sql.DriverManager; |
5 | 7 | import java.sql.ResultSet; |
@@ -27,25 +29,24 @@ public static void main(String[] args) { |
27 | 29 | Connection conn = null; |
28 | 30 | try { |
29 | 31 | // 加载MySQL的数据驱动程序 |
30 | | - Class.forName("com.mysql.jdbc.Driver"); |
| 32 | +// Class.forName("com.mysql.jdbc.Driver"); // jdk1.6 之后可以不用再调用这句 |
| 33 | +// DriverManager.registerDriver(new Driver()); |
31 | 34 | // 创建数据连接对象 |
32 | 35 | conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); |
33 | 36 | // 创建Statement对象 |
34 | 37 | Statement statement = conn.createStatement(); |
35 | | - |
36 | | - // 执行一条sql |
| 38 | + // 执行sql |
37 | 39 | // 通过execuUpdate()方法用来数据的更新,包括插入和删除等操; |
38 | | - // 通过xecuteQuery()方法进行数据的查询,而查询结果会得到 ResulSet对象 |
39 | | - statement.executeUpdate( "INSERT INTO user(gmt_create, gmt_modified,user_name)" + " VALUES (now(),now(),'jdbcTest') ") ; |
| 40 | + String userName = "tst"+ System.currentTimeMillis(); |
| 41 | + statement.executeUpdate( "INSERT INTO user(gmt_create, gmt_modified,user_name)" + " VALUES (now(),now(),'"+userName+"') ") ; |
| 42 | + // 通过executeQuery()方法进行数据的查询,而查询结果会得到 ResulSet对象 |
40 | 43 | ResultSet resultSel = statement.executeQuery( "select * from user" ); |
41 | 44 | while(resultSel.next()){ |
42 | 45 | String username = resultSel.getString("user_name"); |
43 | | - System.out.println(username); |
| 46 | + System.out.println("userName:"+username); |
44 | 47 | } |
45 | 48 | conn.close(); |
46 | | - } catch (ClassNotFoundException e) { |
47 | | - System.out.println(e); |
48 | | - } catch (SQLException e) { |
| 49 | + } catch (Exception e) { |
49 | 50 | System.out.println(e); |
50 | 51 | }finally{ |
51 | 52 | if(conn!=null){ |
|
0 commit comments