JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫實例代碼
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; } }
進行SqlServe數(shù)據(jù)庫java_conn_test中的tb_User進行數(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA 2017.1.4 x64配置步驟(介紹)
下面小編就為大家?guī)硪黄狪ntelliJ IDEA 2017.1.4 x64配置步驟(介紹)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Java 異常java.lang.NoSuchFieldException解決方案
這篇文章主要介紹了Java 異常java.lang.NoSuchFieldException解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10windows10 JDK安裝及配置環(huán)境變量與Eclipse安裝教程
這篇文章主要介紹了windows10 JDK安裝及配置環(huán)境變量與Eclipse安裝,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10Spring @RestController注解組合實現(xiàn)方法解析
這篇文章主要介紹了Spring @RestController注解組合實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06Spring學(xué)習(xí)通過AspectJ注解方式實現(xiàn)AOP操作
這篇文章主要為大家介紹了Spring學(xué)習(xí)通過AspectJ注解方式實現(xiàn)AOP操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05解決SpringMVC Controller 接收頁面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問題
下面小編就為大家分享一篇解決SpringMVC Controller 接收頁面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03