1 import java.io.IOException;
2 import java.io.InputStream;
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.Statement;
7 import java.util.Properties;
8
9 public class JdbcMySql {
10
11 public static Properties getProperties() {
12 Properties props = new Properties();
13 InputStream is = null;
14 try {
15 is = JdbcMySql.class.getResourceAsStream("/jdbc_mysql.properties");
16 props.load(is);
17 } catch (Exception e) {
18 e.printStackTrace();
19 return null;
20 } finally {
21 if(is != null)
22 try {
23 is.close();
24 } catch (IOException e) {
25 e.printStackTrace();
26 }
27 }
28 return props;
29 }
30
31 public static void main(String[] args) {
32 Connection conn = null;
33 Statement stmt = null;
34 ResultSet rs = null;
35
36 Properties props = getProperties();
37 if (props != null) {
38 String driver = props.getProperty("driver");
39 String url = props.getProperty("url");
40 String user = props.getProperty("user");
41 String password = props.getProperty("password");
42 String characterEncoding = props.getProperty("characterEncoding");
43
44 try {
45 Class.forName(driver);
46 conn = DriverManager.getConnection(url + "?characterEncoding=" + characterEncoding, user, password);
47 stmt = conn.createStatement();
48
49 String sql = "delete from student where id = '2'";
50 stmt.executeUpdate(sql);
51
52 sql = "select * from student";
53 rs = stmt.executeQuery(sql);
54
55 while (rs.next()) {
56 System.out.print(rs.getInt(1) + "\t");
57 System.out.print(rs.getString(2) + "\t");
58 System.out.print(rs.getString(3) + "\n");
59 }
60 } catch (Exception e) {
61 e.printStackTrace();
62 } finally {
63 try {
64 if (rs != null)
65 rs.close();
66 if (stmt != null)
67 stmt.close();
68 if (conn != null)
69 conn.close();
70 } catch (Exception ex) {
71 ex.printStackTrace();
72 }
73 }
74 }
75 }
76 }