JDBC初步 JDBC連接SQLServer 2008之心路歷程
Jun 07, 2016 pm 03:51 PMJDBC簡(jiǎn)介 今天,研究了下JDBC連接SQL Server2008的過(guò)程,為了日后學(xué)習(xí)的方便,就把自己的心得寫(xiě)出來(lái),供自己日后參考,如果這篇文章對(duì)您有所幫助,那就更好了。 首先,大家需要知道的是JDBC僅僅是一個(gè)接口,通過(guò)JDBC去加載對(duì)應(yīng)的驅(qū)動(dòng)程序,進(jìn)而來(lái)操作數(shù)據(jù)庫(kù)
? ? ? JDBC簡(jiǎn)介
? ? ? ?今天,研究了下JDBC連接SQL Server2008的過(guò)程,為了日后學(xué)習(xí)的方便,就把自己的心得寫(xiě)出來(lái),供自己日后參考,如果這篇文章對(duì)您有所幫助,那就更好了。
?????? 首先,大家需要知道的是JDBC僅僅是一個(gè)接口,通過(guò)JDBC去加載對(duì)應(yīng)的驅(qū)動(dòng)程序,進(jìn)而來(lái)操作數(shù)據(jù)庫(kù)。換句話說(shuō),JDBC需要依賴(lài)對(duì)應(yīng)數(shù)據(jù)庫(kù)的驅(qū)動(dòng)程序才可以訪問(wèn)數(shù)據(jù)庫(kù)。所以,當(dāng)我們?cè)跈C(jī)器上安裝了一個(gè)數(shù)據(jù)庫(kù)后,應(yīng)用程序是不能直接操作數(shù)據(jù)庫(kù)的,只有安裝了對(duì)應(yīng)的驅(qū)動(dòng)程序后,應(yīng)用程序才可以通過(guò)驅(qū)動(dòng)程序去操作數(shù)據(jù)庫(kù)。
? ? ? ?JDBC全稱(chēng):
? ? ? ? ? ? Java? Data?? Base? Connectivity(java數(shù)據(jù)庫(kù)連接)
? ? ? ? ? ? 組成JDBC的2個(gè)包:
? ? ? ? ? ?Java.sql和javax.sql
開(kāi)發(fā)JDBC應(yīng)用時(shí)除了需要以上2個(gè)包的支持外,還需要導(dǎo)入相應(yīng)JDBC的數(shù)據(jù)庫(kù)實(shí)現(xiàn)(即數(shù)據(jù)庫(kù)驅(qū)動(dòng))
? ? ? ?JDBC案例實(shí)戰(zhàn)
? ? ? 下面我們通過(guò)一個(gè)簡(jiǎn)單的增刪改查案例來(lái)看下JDBC如何與數(shù)據(jù)庫(kù)交互,今天我們探討的是與SQL Server2008如何交互。因?yàn)镾QL Server2000的服務(wù)端口默認(rèn)為1433 ? ? ? ? 不需要配置。而2008的端口為動(dòng)態(tài)端口,所以我們以2008為例講解。
? ? ? ? 第一步:搭建實(shí)驗(yàn)環(huán)境
? ? ? ? ?1,???????? 在SQL server 2008中創(chuàng)建一個(gè)Myschool數(shù)據(jù)庫(kù),并創(chuàng)建Admin表
? ? ? ? ? ? ? Admin表中就兩個(gè)字段(LoginId,LoginPwd),并且都是nvarchar類(lèi)型,由于這個(gè)過(guò)程比較簡(jiǎn)單,我們就不再贅述。
? ? ? ? ?2,???????? 編寫(xiě)程序,在程序中加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)
? ? ? ? ? ? ? 我們打開(kāi)Eclipse或者M(jìn)yeclipse發(fā)開(kāi)環(huán)境,新建一個(gè)java項(xiàng)目。
? ? ? ? ? ? ? 我這里以Myeclipse 10.0為例講解。
? ? ? ? ? ? ? 新建一個(gè)java項(xiàng)目,命名為javaJDBC,新建一個(gè)Java類(lèi),命名為MyFirstCon.java,
? ? ? ? ? ? ? 然后對(duì)項(xiàng)目javaJDBC點(diǎn)擊右鍵添加一個(gè)lib文件夾,將sqljdbc4.jar復(fù)制到lib文件夾中。對(duì)sqljdbc4點(diǎn)右鍵,出現(xiàn)如下菜單
? ? ? ? ? ? ??
點(diǎn)擊Add? to? Build? Path,然后導(dǎo)入如下命名空間:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
接下來(lái),我們就可以編寫(xiě)代碼了,但是上面我們提到SQL Server 2008中端口為動(dòng)態(tài)的。那么如何使端口成為固定端口呢?我們需要設(shè)定一下:
找到Sql Server下的配置管理器,找到SQL server 網(wǎng)絡(luò)配置,然后找到本機(jī)上安裝的數(shù)據(jù)庫(kù)實(shí)例,如下圖
? ? ? ? ??
? ?這里我們選中Happy的協(xié)議,然后雙擊右側(cè)列表中的Tcp/IP,切換到IP地址選項(xiàng)卡,
? ? ? ? ??
找到IP地址為127.0.0.1的IP地址,將已啟動(dòng)屬性設(shè)置為是,
然后找到IPAll節(jié)點(diǎn),如下圖
? ? ? ? ?
然后點(diǎn)擊應(yīng)用,最后點(diǎn)擊確定就可以了。
然后,按照下圖設(shè)置,
?
將已啟用屬性設(shè)置成是。
切記:設(shè)置完成后要重新啟動(dòng)SQL Server服務(wù)。
二、編寫(xiě)代碼
? 以下代碼均寫(xiě)在Main方法中
?Connection con=null;
- 加載驅(qū)動(dòng)
try {
???? Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); ??
?? } catch (Exception e) {
???? e.printStackTrace();
? }????????????????????
2,獲取與數(shù)據(jù)庫(kù)連接
con=DriverManager.getConnection("jdbc:sqlserver://happy\\happy:1435;DatabaseName=Myschool","sa","123");
3, 獲取用于向sql發(fā)送命令的statement
Statement? stmt=null;
stmt=con.createStatement();
4, 向數(shù)據(jù)庫(kù)發(fā)送SQL命令
String sql="insert into admin values('1112','1112') ";
if(stmt.executeUpdate(sql)>0){
???????????? System.out.println("新增數(shù)據(jù)成功");
??????? }else {
???????????? System.out.println("新增數(shù)據(jù)失?。?);
?????? ? }
5.關(guān)閉連接
Stmt.close()
Con.close();
好了,第一個(gè)新增的案例完成。
在講解下述內(nèi)容前,我們先來(lái)看下Statement的常用方法
Statement常用方法
01.executeQuery(String sql):執(zhí)行sql查詢(xún)并獲取到ResultSet對(duì)象,返回值為ResultSet
02.executeUpdate(String sql):可以執(zhí)行插入、刪除、更新等操作,返回值是執(zhí)行該操作所影響的行數(shù),int類(lèi)型。
03,execute(String sql):可以執(zhí)行任意SQL語(yǔ)句,然后獲得一個(gè)布爾值,表示是否返回Result。
/***************************Update*****************************/
//更新
String sql="update admin set loginpwd='111' where loginid='112'";
stmt.executeUpdate(sql);
/*************************delete*****************************/
//刪除
String sql="delete from admin where loginid='1112'";
stmt.executeUpdate(sql);
/*************************select*****************************/
//查詢(xún)
String sql="select * from admin";
? ?? ResultSet rs=stmt.executeQuery(sql);
? ?? while(rs.next()){
??????? ?? System.out.println(rs.getString("loginid"));
??????? ?? System.out.println(rs.getString("loginpwd"));
??????? ??
? ?? }
PreparedStatement接口
PreparedStatement接口繼承Statement接口,比普通的Statement對(duì)象使用起來(lái)更加靈活,更有效率,使用方式和Statement相同
? ? ? 這次分享就到這里,如有需要,可以聯(lián)系我,email:yymqqc@126.com
?
?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

The problem that SQL Server cannot be reinstalled due to incomplete deletion can be solved by following the following steps: manually delete files and registry entries; use SQL Server installation and uninstall tools; use third-party uninstall tools; check Windows Event Viewer; restart the computer; reinstall SQL Server.

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

SQL Server English installation can be changed to Chinese by following the following steps: download the corresponding language pack; stop the SQL Server service; install the language pack; change the instance language; change the user interface language; restart the application.
