[JAVA100例]053、載入JDBC驅動

來源:互聯網
上載者:User

import java.sql.*;
/**
* <p>Title: JDBC串連資料庫</p>
* <p>Description: 本執行個體示範如何使用JDBC串連Oracle資料庫,並示範添加資料和查詢資料。</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: JDBCConn.java</p>
* @version 1.0
*/
public class JDBCConn{
 private String url="";
 private String username="";
 private String password="";
/**
*<br>方法說明:獲得資料連線
*<br>輸入參數:
*<br>傳回型別:Connection 連線物件
*/
 public Connection conn(){
   try {
   //載入JDBC驅動
    Class.forName("oracle.jdbc.driver.OracleDriver");
    //建立資料庫連接
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL", "test", "test");
    return con;
  }catch(ClassNotFoundException cnf){
   System.out.println("driver not find:"+cnf);
   return null;
  }catch(SQLException sqle){
   System.out.println("can´t connection db:"+sqle);
   return null;
  } catch (Exception e) {
    System.out.println("Failed to load JDBC/ODBC driver.");
    return null;
   }
 }
/**
*<br>方法說明:執行查詢SQL語句
*<br>輸入參數:Connection con 資料庫連接
*<br>輸入參數:String sql 要執行的SQL語句
*<br>傳回型別:
*/
 public void query(Connection con, String sql){
  try{
   if(con==null){
    throw new Exception("database connection can´t use!");
   }
   if(sql==null) throw new Exception("check your parameter: ´sql´! don´t input null!");
   //聲明語句
   Statement stmt = con.createStatement();
   //執行查詢
   ResultSet rs = stmt.executeQuery(sql);
   ResultSetMetaData rmeta = rs.getMetaData();
   //獲得資料欄位個數
   int numColumns = rmeta.getColumnCount();
   while(rs.next())
 {
  for(int i = 0;i< numColumns;i++)
  {
 String sTemp = rs.getString(i+1);
 System.out.print(sTemp+" ");
  }
  System.out.println("");
 }
  }catch(Exception e){
   System.out.println("query error:"+e);
  }
 }
/**
*<br>方法說明:執行插入、更新、刪除等沒有返回結果集的SQL語句
*<br>輸入參數:Connection con 資料庫連接
*<br>輸入參數:String sql 要執行的SQL語句
*<br>傳回型別:
*/
  public void execute(Connection con, String sql){
  try{
   if(con==null) return;
   Statement stmt = con.createStatement();
  stmt.executeUpdate(sql);
}catch(Exception e){
   System.out.println("execute error: sql = "+sql);
   System.out.println(e);
  }
 }
/**
*<br>方法說明:執行個體示範
*<br>輸入參數:
*<br>傳回型別:
*/
 public void demo(){
  try{
   JDBCConn oc = new JDBCConn();
   Connection conn = oc.conn();
   String sql = "insert into TBL_USER(id,name,password)values(seq_user.nextval,´switch´,´haorenpingan´)";
   oc.execute(conn,sql);
   sql = "select * from TBL_USER";
   oc.query(conn,sql);
   conn.close();
  }catch(SQLException se){
   System.out.println(se);
  }catch(Exception e){
   System.out.println(e);
  }

 }
/**
*<br>方法說明:主方法
*<br>輸入參數:
*<br>傳回型別:
*/
 public static void main(String[] arg){
  if(arg.length!=3){
   System.out.println("use: java JDBCConn url username password");
   return;
  }
  JDBCConn oc = new JDBCConn();
  oc.url = arg[0];
  oc.username=arg[1];
  oc.password=arg[2];
  oc.demo();
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.