JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫(kù)實(shí)例代碼
JDBC(Java Data Base Connectivity,Java數(shù)據(jù)庫(kù)連接)是一種用于執(zhí)行SQL語(yǔ)句的Java API,可以為多種關(guān)系數(shù)據(jù)庫(kù)提供統(tǒng)一訪(fǎng)問(wèn),它由一組用Java語(yǔ)言編寫(xiě)的類(lèi)和接口組成。JDBC提供了一種基準(zhǔn),據(jù)此可以構(gòu)建更高級(jí)的工具和接口,使數(shù)據(jù)庫(kù)開(kāi)發(fā)人員能夠編寫(xiě)數(shù)據(jù)庫(kù)應(yīng)用程序。 JDBC并不能直接訪(fǎng)問(wèn)數(shù)據(jù)庫(kù),需要借助于數(shù)據(jù)庫(kù)廠(chǎng)商提供的JDBC驅(qū)動(dòng)程序。
數(shù)據(jù)庫(kù)連接
如果要在Java訪(fǎng)問(wèn)數(shù)據(jù)庫(kù),首先要加載一個(gè)數(shù)據(jù)庫(kù)驅(qū)動(dòng),數(shù)據(jù)庫(kù)驅(qū)動(dòng)只需要在第一次訪(fǎng)問(wèn)時(shí)加載一次。然后再每次訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)時(shí)創(chuàng)建一個(gè)Connection實(shí)例,獲取數(shù)據(jù)庫(kù)連接,這樣就可以執(zhí)行操作數(shù)據(jù)庫(kù)的SQL語(yǔ)句。最后用完后釋放掉數(shù)據(jù)庫(kù)的連接。
數(shù)據(jù)庫(kù)驅(qū)動(dòng)類(lèi)
不同的數(shù)據(jù)庫(kù)實(shí)現(xiàn)JDBC接口不同,所以就產(chǎn)生了不同的數(shù)據(jù)庫(kù)驅(qū)動(dòng)包。驅(qū)動(dòng)包就包含一些負(fù)責(zé)數(shù)據(jù)庫(kù)連接的類(lèi),把我們要操作的SQL語(yǔ)句傳遞到里面去。我的PC用的是SQL2012,所以我們要去這里http://www.microsoft.com/zh-cn/search/DownloadResults.aspx?q=jdbc下載驅(qū)動(dòng)
下完后在新建的java_project導(dǎo)入驅(qū)動(dòng)包
右擊選中項(xiàng)目>>Build Path >>Add External Archives... 選中下載解壓的文件
導(dǎo)入成功后的項(xiàng)目:
package com.Project_DataBase01; import java.sql.Connection; import java.sql.DriverManager; public class SelectQuery { private Connection conn; /* * 創(chuàng)建一個(gè)返回Connection的方法 */ public Connection getConnection(){ try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); System.out.println("數(shù)據(jù)庫(kù)驅(qū)動(dòng)加載成功"); conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=java_conn_test","sa","123456"); if(conn==null){ System.out.println("數(shù)據(jù)庫(kù)連接失敗"); System.out.println("-----------------------"); }else { System.out.println("數(shù)據(jù)庫(kù)連接成功"); System.out.println("-----------------------"); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return conn; } }
進(jìn)行SqlServe數(shù)據(jù)庫(kù)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("請(qǐng)檢查數(shù)據(jù)庫(kù)連接"); 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("查詢(xún)成功"); System.out.println("-----------------------"); } catch (Exception e) { // TODO: handle exception System.out.println("查詢(xún)失敗"); System.out.println("-----------------------"); } } else { System.out.println("請(qǐng)檢查數(shù)據(jù)庫(kù)連接"); 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("請(qǐng)檢查數(shù)據(jù)庫(kù)連接"); 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("請(qǐng)檢查數(shù)據(jù)庫(kù)連接"); System.out.println("-----------------------"); } } }
運(yùn)行程序:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA 2017.1.4 x64配置步驟(介紹)
下面小編就為大家?guī)?lái)一篇IntelliJ IDEA 2017.1.4 x64配置步驟(介紹)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06Java 異常java.lang.NoSuchFieldException解決方案
這篇文章主要介紹了Java 異常java.lang.NoSuchFieldException解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10windows10 JDK安裝及配置環(huán)境變量與Eclipse安裝教程
這篇文章主要介紹了windows10 JDK安裝及配置環(huán)境變量與Eclipse安裝,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10Spring @RestController注解組合實(shí)現(xiàn)方法解析
這篇文章主要介紹了Spring @RestController注解組合實(shí)現(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Spring學(xué)習(xí)通過(guò)AspectJ注解方式實(shí)現(xiàn)AOP操作
這篇文章主要為大家介紹了Spring學(xué)習(xí)通過(guò)AspectJ注解方式實(shí)現(xiàn)AOP操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05解決SpringMVC Controller 接收頁(yè)面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問(wèn)題
下面小編就為大家分享一篇解決SpringMVC Controller 接收頁(yè)面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03