1 package comparejsonandmysql;
 2 
 3 import java.sql.*;
 4 import java.text.DateFormat;
 5 import java.text.ParseException;
 6 import java.text.SimpleDateFormat;
 7 import java.util.*;
 8 
 9 
10 public class MysqlTest {
11     public static void main(String[] args) {
12         String driver = "com.mysql.jdbc.Driver";
13         String url = "jdbc:mysql://127.0.0.1:3306/phpwind";
14         String user = "root";
15         String passwd = "123456";
16         int i = 1;
17         try {
18             Class.forName(driver);
19             Connection conn = DriverManager.getConnection(url,user,passwd);
20             if(!conn.isClosed()) {
21                 System.out.println("Succeed Connected!");
22                 String query = "select * from pw_acloud_apis";
23                 Statement statement = conn.createStatement();
24                 ResultSet rs = statement.executeQuery(query);
25                 System.out.println("------------------------");
26                 System.out.println("执行结果如下");
27                 System.out.println("------------------------");
28                 while(rs.next()) {
29                     System.out.print(rs.getInt(1) +"\t");//列数是从 1 开始
30                     System.out.println(rs.getString(2));
31                 }
32                 //更新表数据
33                 String creaDate = "20171212";
34                 String modiDate = "20171213";
35                 SimpleDateFormat sdf = new  SimpleDateFormat("yyyymmdd");
36                 
37                 String pQuery = "insert into pw_acloud_app_configs(app_id, app_key, app_value, app_type, created_time, modified_time)" + "values(?,?,?,?,?,?)";
38                 PreparedStatement pstment = conn.prepareStatement(pQuery);
39                 pstment.setInt(1, 3);
40                 pstment.setString(2, "key1");
41                 pstment.setString(3, "hello world!");
42                 pstment.setInt(4, 1);
43                 pstment.setLong(5, sdf.parse(creaDate).getTime());
44                 pstment.setLong(6, sdf.parse(modiDate).getTime());
45                 pstment.executeUpdate();
46                 System.out.println("inserted done!");
47                 
48                 //删除表数据:
49                 String pDel = "Delete from pw_acloud_apis where id = ?";
50                 PreparedStatement ps = conn.prepareStatement(pDel);
51                 ps.setInt(1, 2);
52                 ps.executeUpdate();
53                 System.out.println("Delete Done!");
54                 rs.close();
55                 statement.close();
56                 conn.close();
57             }
58             
59     
60         } catch (ClassNotFoundException e) {
61             // TODO Auto-generated catch block
62             System.out.println("JDBC Drive not found!");
63             e.printStackTrace();
64         }catch (SQLException e) {
65             // TODO Auto-generated catch block
66             System.out.println("Fail to connect!");
67             e.printStackTrace();
68         } catch (ParseException e) {
69             // TODO Auto-generated catch block
70             e.printStackTrace();
71         }
72     }
73 }