經過 JDBC 向指定的數據表中插入一條記錄,查詢記錄

//經過 JDBC 向指定的數據表中插入一條記錄
 /*
  * 1.Statement: 用於執行SQl語句的對象
  *  經過Connection 的createStatement()方法來獲取
  *  經過executeUptate(sql) 可執行sql語句
  *  傳入的sql 能夠是 insert , update, delete 但不能是select
  * 2.Connection 和 Statement 須要關閉
  *  須要在finally中關閉
  * 3.關閉的順序:先關閉後獲取的,即先關閉 Statement 後關閉 Connection
  */
 @Test
 public void testStatement() throws Exception{
  //1.獲取數據庫鏈接
  Connection conn;
  Statement statement;
  conn = null;
  statement = null;
  try {
   
   conn = getConnection();
   //3.SQL語句
   String sql =
     "insert into mytable values(23454, 'BBB')";
   //4.執行
   //1) 獲取操做SQL語句的Statement對象:調用Connection 的createStatement()方法
   statement = conn.createStatement();
   //2) 調用 Statement 對象的executeUpdate(sql)執行 SQL語句進行插入
   statement.executeUpdate(sql);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   
   try {
    if(statement != null)
     //5. 關閉 Statement對象
     statement.close();
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }finally{
    if(conn != null)
     //2.關閉鏈接
     conn.close();
   }
  }
}
sql

public Connection getConnection() throws Exception{
  String driverClass = null;
  String jdbcUrl = null;
  String user = null;
  String password = null;
  
  InputStream in =
    getClass().getClassLoader().getResourceAsStream("jdbc.properties");
  Properties properties = new Properties();
  properties.load(in);
  driverClass = properties.getProperty("driver");
  jdbcUrl = properties.getProperty("jdbcUrl");
  user = properties.getProperty("user");
  password = properties.getProperty("password"); 
數據庫

  Driver driver =
    (Driver) Class.forName(driverClass).newInstance();
  
  Properties info = new Properties();
  info.put("user", user);
  info.put("password", password);
  Connection connection = driver.connect(jdbcUrl, info);
  
  return connection;
 }
服務器

 

/*ResultSet: 結果集。封裝了使用 JDBC 進行查詢的結果
  * 1.調用 Statement 對象的 executeQuery(sql) 能夠獲得結果集。
  * 2.ResultSet 返回的實際上就是一張數據表。有一個指針指向數據表的第一行的前面
  * 能夠調用 next()方法檢測下一行是否有效。如有效該方法返回 true,且指針下移至關於
  * Iterator 對象的 hasNext() 和next()方法的結合體
  * 3.當指針對位到一行時,能夠經過調用getXxx(index) 或 getXxx(columnName)來
  * 獲取每一列的值。例如:getInt(12345),getString(「name」)
  * 4.ResultSet 固然也須要關閉
  * */
 @Test
 public void testResultSet(){
  Connection conn = null;
  Statement statement = null;
  ResultSet rs = null;
  
  try {
   conn = JDBCTools.GetConnection();
   statement = conn.createStatement();
   String sql = "select id, name from mytable";
   rs = statement.executeQuery(sql);
   
   while(rs.next()){
    int id = rs.getInt(1);
    String name = rs.getString("name");
    
    System.out.println(id);
    System.out.println(name);
   }
   
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   JDBCTools.release(rs, statement, conn);
  }
 }
spa

public class JDBCTools {
 public static void release(ResultSet rs, Statement statement, Connection conn){
  if(rs != null){
   try {
    rs.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  if(statement != null)
   try {
    statement.close();
   } catch (Exception e2) {
    e2.printStackTrace();
   }
  if(conn != null)
   try {
    conn.close();
   } catch (Exception e2) {
    e2.printStackTrace();
   }
 }
 
 /*
  *關閉Statement 和 Connection */
 public static void release(Statement statement, Connection conn){
  if(statement != null)
   try {
    statement.close();
   } catch (Exception e2) {
    e2.printStackTrace();
   }
  if(conn != null)
   try {
    conn.close();
   } catch (Exception e2) {
    e2.printStackTrace();
   }
 }.net

 /*
  * 1. 獲取鏈接的方法
  * 經過讀配置文件從數據庫服務器獲取一個鏈接
  */
 public static Connection GetConnection() throws Exception{
  String driverClass = null;
  String jdbcUrl = null;
  String user = null;
  String password = null;
  
  InputStream in =
    JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
  Properties properties = new Properties();
  properties.load(in);
  driverClass = properties.getProperty("driver");
  jdbcUrl = properties.getProperty("jdbcUrl");
  user = properties.getProperty("user");
  password = properties.getProperty("password");
  
  
  Driver driver =
    (Driver) Class.forName(driverClass).newInstance();
  
  Properties info = new Properties();
  info.put("user", user);
  info.put("password", password);
  Connection connection = driver.connect(jdbcUrl, info);
  
  return connection;
 }
}
指針

相關文章
相關標籤/搜索