JDBC

1.

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 import java.sql.Statement;
 6 
 7 /**
 8  * JDBC Read
 9  * 
10  * @author y
11  */
12 public class ReadTest {
13     public static void main(String[] args) {
14 
15         Connection conn = null;
16         Statement stmt = null;
17         ResultSet rs = null;
18 
19         try {
20             // load the driver
21             Class.forName("com.mysql.jdbc.Driver");
22 
23             // connect to the database
24             String url = "jdbc:mysql://localhost:3306/t_jdbc_y";
25             String user = "root";
26             String password = "root";
27             conn = DriverManager.getConnection(url, user, password);
28 
29             // execute the SQL
30             stmt = conn.createStatement();
31             // stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
32             String sql = "select * from heros";
33             rs = stmt.executeQuery(sql);
34             while (rs.next()) {
35                 System.out.println(rs.getInt("id") + "|" + rs.getString("name")
36                         + "|" + rs.getString("type"));
37             }
38             // show the special result
39             // backward
40             rs.previous();
41             // location
42             rs.absolute(1);
43         } catch (ClassNotFoundException e) {
44             e.printStackTrace();
45         } catch (SQLException e) {
46             e.printStackTrace();
47         }
48 
49         finally {
50             try {
51                 // close resource
52                 if (null != rs) {
53                     rs.close();
54                 }
55                 if (null != stmt) {
56                     stmt.close();
57                 }
58                 if (null != conn) {
59                     conn.close();
60                 }
61 
62             } catch (SQLException e) {
63                 e.printStackTrace();
64             }
65         }
66 
67     }
68 }

2.

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.SQLException;
 4 import java.sql.Statement;
 5 
 6 /**
 7  * JDBC Create
 8  * 
 9  * @author y
10  */
11 public class CreateTest {
12     public static void main(String[] args) {
13 
14         Connection conn = null;
15         Statement stmt = null;
16 
17         try {
18             // load the driver
19             Class.forName("com.mysql.jdbc.Driver");
20 
21             // connect to the database
22             String url = "jdbc:mysql://localhost:3306/t_jdbc_y?useUnicode=true&characterEncoding=UTF-8";
23             String user = "root";
24             String password = "root";
25             conn = DriverManager.getConnection(url, user, password);
26 
27             // execute the SQL
28             stmt = conn.createStatement();
29             String sql = "insert into heros values(8,'dk','力量')";
30             int flag = stmt.executeUpdate(sql);
31             if(flag == 1) {
32                 System.out.println("成功插入一条数据");
33             } else {
34                 System.out.println("数据插入失败");
35             }
36         } catch (ClassNotFoundException e) {
37             e.printStackTrace();
38         } catch (SQLException e) {
39             e.printStackTrace();
40         }
41         
42         finally {
43             try {
44                 // close resource
45                 if (null != stmt) {
46                     stmt.close();
47                 }
48                 if (null != conn) {
49                     conn.close();
50                 }
51 
52             } catch (SQLException e) {
53                 e.printStackTrace();
54             }
55         }
56 
57     }
58 }

3.

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.SQLException;
 4 import java.sql.Statement;
 5 
 6 /**
 7  * JDBC Create
 8  * 
 9  * @author y
10  */
11 public class CreateWithParamsTest {
12     public static void main(String[] args) {
13         
14         if (args.length != 3) {
15             System.out.println("please input the correct params");
16         }
17         
18         
19         Connection conn = null;
20         Statement stmt = null;
21 
22         try {
23             // load the driver
24             Class.forName("com.mysql.jdbc.Driver");
25 
26             // connect to the database
27             String url = "jdbc:mysql://localhost:3306/t_jdbc_y?useUnicode=true&characterEncoding=UTF-8";
28             String user = "root";
29             String password = "root";
30             conn = DriverManager.getConnection(url, user, password);
31 
32             // execute the SQL
33             stmt = conn.createStatement();
34             int id = Integer.parseInt(args[0]);
35             String name = args[1];
36             String type = args[2];
37             String sql = "insert into heros values(" + id + ",'" + name + "','" + type + "')"; 
38             int flag = stmt.executeUpdate(sql);
39             if(flag == 1) {
40                 System.out.println("成功插入一条数据");
41             } else {
42                 System.out.println("数据插入失败");
43             }
44         } catch (ClassNotFoundException e) {
45             e.printStackTrace();
46         } catch (SQLException e) {
47             e.printStackTrace();
48         }
49         
50         finally {
51             try {
52                 // close resource
53                 if (null != stmt) {
54                     stmt.close();
55                 }
56                 if (null != conn) {
57                     conn.close();
58                 }
59 
60             } catch (SQLException e) {
61                 e.printStackTrace();
62             }
63         }
64 
65     }
66 }

4.

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.PreparedStatement;
 4 import java.sql.SQLException;
 5 
 6 /**
 7  * JDBC PreparedStatement
 8  * 
 9  * @author y
10  */
11 public class PrepStmtTest {
12     public static void main(String[] args) {
13 
14         Connection conn = null;
15         PreparedStatement preStmt = null;
16 
17         try {
18             // load the driver
19             Class.forName("com.mysql.jdbc.Driver");
20 
21             // connect to the database
22             String url = "jdbc:mysql://localhost:3306/t_jdbc_y?useUnicode=true&characterEncoding=UTF-8";
23             String user = "root";
24             String password = "root";
25             conn = DriverManager.getConnection(url, user, password);
26 
27             // execute the SQL
28             String sql = "insert into heros values(?, ?, ?)";
29             preStmt = conn.prepareStatement(sql);
30             preStmt.setInt(1, 6);
31             preStmt.setString(2, "loa");
32             preStmt.setString(3, "力量");
33             int flag = preStmt.executeUpdate();
34             if (flag == 1) {
35                 System.out.println("成功插入一条数据");
36             } else {
37                 System.out.println("数据插入失败");
38             }
39         } catch (ClassNotFoundException e) {
40             e.printStackTrace();
41         } catch (SQLException e) {
42             e.printStackTrace();
43         }
44 
45         finally {
46             try {
47                 // close resource
48                 if (null != preStmt) {
49                     preStmt.close();
50                 }
51                 if (null != conn) {
52                     conn.close();
53                 }
54 
55             } catch (SQLException e) {
56                 e.printStackTrace();
57             }
58         }
59 
60     }
61 }

5.

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.SQLException;
 4 import java.sql.Statement;
 5 
 6 /**
 7  * JDBC Batch
 8  * 
 9  * @author y
10  */
11 public class BatchTest {
12     public static void main(String[] args) {
13 
14         Connection conn = null;
15         Statement stmt = null;
16 
17         try {
18             // load the driver
19             Class.forName("com.mysql.jdbc.Driver");
20 
21             // connect to the database
22             String url = "jdbc:mysql://localhost:3306/t_jdbc_y?useUnicode=true&characterEncoding=UTF-8";
23             String user = "root";
24             String password = "root";
25             conn = DriverManager.getConnection(url, user, password);
26 
27             // execute the SQL
28             stmt = conn.createStatement();
29             String sql1 = "insert into heros values(13,'dk','力量')";
30             String sql2 = "insert into heros values(14,'dk','力量')";
31             String sql3 = "insert into heros values(15,'dk','力量')";
32             
33             stmt.addBatch(sql1);
34             stmt.addBatch(sql2);
35             stmt.addBatch(sql3);
36             
37             int[] flags = stmt.executeBatch();
38             for(int flag : flags) {
39                 if(1 != flag) {
40                     System.out.println("插入数据失败");
41                 }
42             }
43         } catch (ClassNotFoundException e) {
44             e.printStackTrace();
45         } catch (SQLException e) {
46             e.printStackTrace();
47         }
48         
49         finally {
50             try {
51                 // close resource
52                 if (null != stmt) {
53                     stmt.close();
54                 }
55                 if (null != conn) {
56                     conn.close();
57                 }
58 
59             } catch (SQLException e) {
60                 e.printStackTrace();
61             }
62         }
63 
64     }
65 }

 

posted @ 2014-07-04 09:11  消失的旧时光  阅读(181)  评论(0编辑  收藏  举报