JDBC(Java Data Base Connectivity,Java數(shù)據(jù)庫連接)是一種用于執(zhí)行SQL語句的Java API,可以為多種關(guān)系數(shù)據(jù)庫提供統(tǒng)一訪問,它由一組用Java語言編寫的類和接口組成。JDBC提供了一種基準(zhǔn),據(jù)此可以構(gòu)建更高級的工具和接口,使數(shù)據(jù)庫開發(fā)人員能夠編寫數(shù)據(jù)庫應(yīng)用程序。 JDBC并不能直接訪問數(shù)據(jù)庫,需要借助于數(shù)據(jù)庫廠商提供的JDBC驅(qū)動程序。 
數(shù)據(jù)庫連接
如果要在Java訪問數(shù)據(jù)庫,首先要加載一個數(shù)據(jù)庫驅(qū)動,數(shù)據(jù)庫驅(qū)動只需要在第一次訪問時加載一次。然后再每次訪問數(shù)據(jù)庫時創(chuàng)建一個Connection實例,獲取數(shù)據(jù)庫連接,這樣就可以執(zhí)行操作數(shù)據(jù)庫的SQL語句。最后用完后釋放掉數(shù)據(jù)庫的連接。
數(shù)據(jù)庫驅(qū)動類
不同的數(shù)據(jù)庫實現(xiàn)JDBC接口不同,所以就產(chǎn)生了不同的數(shù)據(jù)庫驅(qū)動包。驅(qū)動包就包含一些負(fù)責(zé)數(shù)據(jù)庫連接的類,把我們要操作的SQL語句傳遞到里面去。我的PC用的是SQL2012,所以我們要去這里http://www.microsoft.com/zh-cn/search/DownloadResults.aspx?q=jdbc下載驅(qū)動
下完后在新建的java_project導(dǎo)入驅(qū)動包

右擊選中項目>>Build Path >>Add External Archives... 選中下載解壓的文件

導(dǎo)入成功后的項目:

package com.Project_DataBase01;import java.sql.Connection;import java.sql.DriverManager;public class SelectQuery {  private Connection conn;  /*  * 創(chuàng)建一個返回Connection的方法  */ public Connection getConnection(){  try {   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");   System.out.println("數(shù)據(jù)庫驅(qū)動加載成功");   conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=java_conn_test","sa","123456");   if(conn==null){    System.out.println("數(shù)據(jù)庫連接失敗");    System.out.println("-----------------------");   }else {    System.out.println("數(shù)據(jù)庫連接成功");    System.out.println("-----------------------");   }  } catch (Exception e) {   // TODO: handle exception   e.printStackTrace();  }  return conn; }} 進(jìn)行SqlServe數(shù)據(jù)庫java_conn_test中的tb_User進(jìn)行數(shù)據(jù)的增刪改查。
 package com.Project_DataBase01;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;public class StartMain {  private static Connection conn; public static void main(String[] args) {  // TODO Auto-generated method stub    conn=new SelectQuery().getConnection();    GetInsert();  GetSelect();    GetUpdate();  GetSelect();    GetDelete();  GetSelect(); } /*  * INSERT  */ public static void GetInsert(){  if(conn!=null){   //INSERT    System.out.println("-----------INSERT------------");   int x=1+(int)(Math.random()*5000);   String insert_str="INSERT INTO tb_User (UserName,UserPwd,UserId) VALUES ('name_"+x+"','pwd_"+x+"',NEWID())";   try {    Statement insertstatement=conn.createStatement();    int result= insertstatement.executeUpdate(insert_str);    if(result>0){     System.out.println("添加成功");     System.out.println("-----------------------");    }    else {          System.out.println("添加失敗");     System.out.println("-----------------------");    }   } catch (Exception e) {    System.out.println("添加失敗");    System.out.println("-----------------------");    // TODO: handle exception   }  }  else {   System.out.println("請檢查數(shù)據(jù)庫連接");   System.out.println("-----------------------");  } }  /*  * SELECT  */ public static void GetSelect(){  if(conn!=null){      //SELECT   System.out.println("-----------SELECT------------");   String select_str=" SELECT * FROM tb_User ";   try {    PreparedStatement selectps=conn.prepareStatement(select_str);    ResultSet rs=selectps.executeQuery();    while (rs.next()) {     String name=rs.getString("UserName");     String pwd=rs.getString("UserPwd");     String UserId=rs.getString("UserId");     System.out.println(name+"/t"+pwd+"/t"+UserId);    }    System.out.println("查詢成功");    System.out.println("-----------------------");   } catch (Exception e) {    // TODO: handle exception    System.out.println("查詢失敗");    System.out.println("-----------------------");   }  }  else {   System.out.println("請檢查數(shù)據(jù)庫連接");   System.out.println("-----------------------");  } } /*  * UPDATE  */ public static void GetUpdate(){  if(conn!=null){   //UPDATE   System.out.println("-----------INSERT------------");   String update_str="UPDATE tb_User SET UserPwd=UserPwd+'xxxxxxxx' WHERE UserId='fa562573-218a-4205-b67d-ebdfac3f8329'";   try {    Statement updatestatement=conn.createStatement();    int result=updatestatement.executeUpdate(update_str);    if(result>0){     System.out.println("修改成功!");     System.out.println("-----------------------");    }else {     System.out.println("修改失敗");     System.out.println("-----------------------");    }   } catch (Exception e) {    // TODO: handle exception    System.out.println("修改失敗");    System.out.println("-----------------------");   }  }  else {   System.out.println("請檢查數(shù)據(jù)庫連接");   System.out.println("-----------------------");  } }  /*  * DELETE  */ public static void GetDelete(){  if(conn!=null){   //DELETE    System.out.println("-----------DELETE------------");   String delete_str="DELETE tb_User WHERE UserId!='fa562573-218a-4205-b67d-ebdfac3f8329'";   try {    Statement deletestatement=conn.createStatement();    int result=deletestatement.executeUpdate(delete_str);    if(result>0){     System.out.println("刪除成功!");     System.out.println("-----------------------");    }else {     System.out.println("刪除失敗");     System.out.println("-----------------------");    }   } catch (Exception e) {    // TODO: handle exception    System.out.println("刪除失敗");    System.out.println("-----------------------");   }  }  else {   System.out.println("請檢查數(shù)據(jù)庫連接");   System.out.println("-----------------------");  } }} 運行程序:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點
疑難解答
圖片精選