JDBC連接SqlServer2005數(shù)據(jù)庫
一、下載安裝
1
、
SqlServer 2005 Express Edition
??
下載:
http://msdn.microsoft.com/vstudio/express/sql/download/
??
安裝完數(shù)據(jù)庫后設(shè)置
ICP/IP
協(xié)議啟動,具體如下:
??
(
1
)打開
SQL Server Configuration Manager
??
(
2
)轉(zhuǎn)到
SQL Server 2005 Network Configuration->Protocols for SQLEXPRESS
??
(
3
)將
TCP/IP
設(shè)置為
Enabled
(啟用)
??
(
4
)雙擊
TCP/IP
項,轉(zhuǎn)到
IP Addresses
頁
??
(
5
)
IP All
中設(shè)置
TCP Port
為
1433
??
(
6
)重新啟動服務(wù)
2
、
SqlServer2005
數(shù)據(jù)庫
JDBC
驅(qū)動
??
下載:
http://download.microsoft.com/download/d/2/e/d2e1ffb6-2cfa-4a62-a22d-a413cce93118/Download_SQL_JDBC_Driver.htm
??
安裝或者解壓,取得
sqljdbc.jar
文件,該文件即為
JDBC
驅(qū)動。將
sqljdbc.jar
放到
classpath
。(
web application
中放在
WEB-INF/lib
下)
二、連接數(shù)據(jù)庫
SqlServer2005
的
java
代碼
1
、在
tempdb
中創(chuàng)建測試數(shù)據(jù)表格
use tempdb
CREATE TABLE dbo.Table_1
(
?ID bigint NOT NULL,
?NAME varchar(20) NOT NULL,
?EMAIL varchar(50) NULL
)? ON [PRIMARY]
2
、測試使用數(shù)據(jù)庫連接
??
下面代碼創(chuàng)建了一個連接到數(shù)據(jù)庫的連接,及使用連接操作數(shù)據(jù)庫。
/*
?*
創(chuàng)建日期
2006-6-1
?*/
package cn.afss.common.web.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
/**
?* @author Amei
?* Amei's FreeSky Studio
?*/
public class TestConnSql2k5Bean {
?/**
? * Log4J Logger for this class
? */
?private static final Logger logger =
? Logger.getLogger(TestConnSql2k5Bean.class);
?private Connection conn = null;
?public TestConnSql2k5Bean() {
? super();
?}
?public void getConnection() {
? try {
?? Class
??? .forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
??? .newInstance();
?? String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=tempdb";
?? String USER = "sa";?//
根據(jù)你自己設(shè)置的數(shù)據(jù)庫連接用戶進(jìn)行設(shè)置
?? String PASSWORD = "sa";?//
根據(jù)你自己設(shè)置的數(shù)據(jù)庫連接密碼進(jìn)行設(shè)置
?? conn = DriverManager.getConnection(URL, USER, PASSWORD);
? } catch (java.lang.ClassNotFoundException ce) {
?? logger.error("Get Connection error:", ce);
? } catch (java.sql.SQLException se) {
?? logger.error("Get Connection error:", se);
? } catch (Exception e) {
?? logger.error("Get Connection error:", e);
? }
?}
?public void testConnection() {
? if (conn == null)
?? this.getConnection();
? try {
?? String sql = "SELECT * FROM TABLE_1";
?? Statement stmt = conn.createStatement();
?? ResultSet rs = stmt.executeQuery(sql);
?? while (rs.next()) {
??? logger.debug(rs.getString("ID"));
??? logger.debug(rs.getString("NAME"));
??? logger.debug(rs.getString("EMAIL"));
?? }
?? rs.close();
?? stmt.close();
? } catch (SQLException e) {
?? logger.error(e.getMessage(), e);
? } finally {
?? if (conn != null)
??? try {
???? conn.close();
??? } catch (SQLException e) {
??? }
? }
?}
?public static void main(String[] args) {
? TestConnSql2k5Bean bean = new TestConnSql2k5Bean();
? bean.testConnection();
?}
}
三、
Sql Server2000
和
2005
的連接代碼區(qū)別
??
寫連接代碼時需要注意
2000
和
2005
的不同:
1
、連接
SqlServer2000
? Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
? URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=tempdb";
2
、連接
SqlServer2005
? Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
? URL = "jdbc:sqlserver://localhost:1433;DatabaseName=tempdb";
posted on 2006-12-01 14:40 寶貝小豬嘜 閱讀(1037) 評論(0) 編輯 收藏 所屬分類: 數(shù)據(jù)庫相關(guān)