<pre name="code" class="java">package chp07; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBC_Test { // 創(chuàng)建靜態(tài)全局變量 static Connection conn; static Statement st; public static void main(String[] args) { insert(); //插入加入記錄 update(); //更新記錄數(shù)據(jù) delete(); //刪除記錄 query(); //查詢記錄并顯示 } /* 插入數(shù)據(jù)記錄,并輸出插入的數(shù)據(jù)記錄數(shù)*/ public static void insert() { conn = getConnection(); // 首先要獲取連接,即連接到數(shù)據(jù)庫 try { String sql = "INSERT INTO staff(name, age, sex,address, depart, worklen,wage)" + " VALUES ('Tom1', 32, 'M', 'china','Personnel','3','3000')"; // 插入數(shù)據(jù)的sql語句 st = (Statement) conn.createStatement(); // 創(chuàng)建用于運行靜態(tài)sql語句的Statement對象 int count = st.executeUpdate(sql); // 運行插入操作的sql語句,并返回插入數(shù)據(jù)的個數(shù) System.out.println("向staff表中插入 " + count + " 條數(shù)據(jù)"); //輸出插入操作的處理結(jié)果 conn.close(); //關(guān)閉數(shù)據(jù)庫連接 } catch (SQLException e) { System.out.println("插入數(shù)據(jù)失敗" + e.getMessage()); } } /* 更新符合要求的記錄,并返回更新的記錄數(shù)目*/ public static void update() { conn = getConnection(); //相同先要獲取連接,即連接到數(shù)據(jù)庫 try { String sql = "update staff set wage='2200' where name = 'lucy'";// 更新數(shù)據(jù)的sql語句 st = (Statement) conn.createStatement(); //創(chuàng)建用于運行靜態(tài)sql語句的Statement對象,st屬局部變量 int count = st.executeUpdate(sql);// 運行更新操作的sql語句,返回更新數(shù)據(jù)的個數(shù) System.out.println("staff表中更新 " + count + " 條數(shù)據(jù)"); //輸出更新操作的處理結(jié)果 conn.close(); //關(guān)閉數(shù)據(jù)庫連接 } catch (SQLException e) { System.out.println("更新數(shù)據(jù)失敗"); } } /* 查詢數(shù)據(jù)庫,輸出符合要求的記錄的情況*/ public static void query() { conn = getConnection(); //相同先要獲取連接,即連接到數(shù)據(jù)庫 try { String sql = "select * from staff"; // 查詢數(shù)據(jù)的sql語句 st = (Statement) conn.createStatement(); //創(chuàng)建用于運行靜態(tài)sql語句的Statement對象,st屬局部變量 ResultSet rs = st.executeQuery(sql); //運行sql查詢語句,返回查詢數(shù)據(jù)的結(jié)果集 System.out.println("最后的查詢結(jié)果為:"); while (rs.next()) { // 推斷是否還有下一個數(shù)據(jù) // 依據(jù)字段名獲取對應(yīng)的值 String name = rs.getString("name"); int age = rs.getInt("age"); String sex = rs.getString("sex"); String address = rs.getString("address"); String depart = rs.getString("depart"); String worklen = rs.getString("worklen"); String wage = rs.getString("wage"); //輸出查到的記錄的各個字段的值 System.out.println(name + " " + age + " " + sex + " " + address + " " + depart + " " + worklen + " " + wage); } conn.close(); //關(guān)閉數(shù)據(jù)庫連接 } catch (SQLException e) { System.out.println("查詢數(shù)據(jù)失敗"); } } /* 刪除符合要求的記錄,輸出情況*/ public static void delete() { conn = getConnection(); //相同先要獲取連接,即連接到數(shù)據(jù)庫 try { String sql = "delete from staff where name = 'lili'";// 刪除數(shù)據(jù)的sql語句 st = (Statement) conn.createStatement(); //創(chuàng)建用于運行靜態(tài)sql語句的Statement對象,st屬局部變量 int count = st.executeUpdate(sql);// 運行sql刪除語句,返回刪除數(shù)據(jù)的數(shù)量 System.out.println("staff表中刪除 " + count + " 條數(shù)據(jù)\n"); //輸出刪除操作的處理結(jié)果 conn.close(); //關(guān)閉數(shù)據(jù)庫連接 } catch (SQLException e) { System.out.println("刪除數(shù)據(jù)失敗"); } } /* 獲取數(shù)據(jù)庫連接的函數(shù)*/ public static Connection getConnection() { Connection con = null; //創(chuàng)建用于連接數(shù)據(jù)庫的Connection對象 try { Class.forName("com.mysql.jdbc.Driver");// 載入Mysql數(shù)據(jù)驅(qū)動 con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/myuser", "root", "root");// 創(chuàng)建數(shù)據(jù)連接 } catch (Exception e) { System.out.println("數(shù)據(jù)庫連接失敗" + e.getMessage()); } return con; //返回所建立的數(shù)據(jù)庫連接 } } |