java連接SqlServer2008的數(shù)據(jù)庫(kù)連接池使用:

創(chuàng)新互聯(lián)建站是一家專業(yè)提供山亭企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、html5、小程序制作等業(yè)務(wù)。10年已為山亭眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Vector;
/**數(shù)據(jù)庫(kù)連接池的公共類 **/
public class ConnectionPool {
private VectorConnection pool;//聲明集合,里面只能是放Connection
/**
* 聲明要的東西
*/
private String url = "jdbc:sqlserver://localhost:1433; database=ajax";
private String username = "sa";
private String password = "sa123";
private String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
/**
* 連接池的大小,也就是連接池中有多少個(gè)數(shù)據(jù)庫(kù)連接
*/
private int poolSize = 5;
private static ConnectionPool instance = null;
/**
* 私有的構(gòu)造方法,禁止外部創(chuàng)建本類的對(duì)象,要想獲得本類的對(duì)象,通過(guò)codegetIstance/code方法
* 使用了設(shè)計(jì)模式中的單子模式
*/
private ConnectionPool() {
init();
}
/**
* 連接池初始化方法,讀取屬性文件的內(nèi)容 建立連接池中的初始連
*/
private void init() {
pool = new VectorConnection(poolSize);
//readConfig();
addConnection();
}
/**
* 返回連接到連接池
*/
public synchronized void release(Connection conn) {
pool.add(conn);
}
/**
* 關(guān)閉連接池中的所有數(shù)據(jù)庫(kù)連接
*/
public synchronized void closePool() {
for (int i = 0; i pool.size(); i++) {
try {
((Connection) pool.get(i)).close();
} catch (SQLException e) {
e.printStackTrace();
}
pool.remove(i);
}
}
/**
* 返回當(dāng)前連接池的對(duì)象
*/
public static ConnectionPool getInstance() {
if (instance == null) {
instance = new ConnectionPool();
}
return instance;
}
/**
* 返回連接池中的一個(gè)數(shù)據(jù)庫(kù)連接
*/
public synchronized Connection getConnection() {
if (pool.size() 0) {
Connection conn = pool.get(0);
pool.remove(conn);
return conn;
} else {
return null;
}
}
/**
* 在連接池中創(chuàng)建初始設(shè)置的的數(shù)據(jù)庫(kù)連接
*/
private void addConnection() {
Connection conn = null;
for (int i = 0; i poolSize; i++) {
try {
Class.forName(driverClassName);
conn = java.sql.DriverManager.getConnection(url, username,
password);
pool.add(conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
1、 安裝時(shí)區(qū)別:
與mysql相比,Sqlserver安裝后,的配置TCP/IP協(xié)議,Sql Server 身份注冊(cè)登錄 ,都比較麻煩。
2、配置連接時(shí)區(qū)別:
mysql
使用的驅(qū)動(dòng):com.mysql.jdbc.Driver
依賴包:mysql 可以直接通過(guò)pom.xml下載
sqlserver驅(qū)動(dòng):com.microsoft.sqlserver.jdbc.SQLServerDriver
依賴包:sqljdbc4 需要從本地置入
%
String sqlDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://localhost:1433;DatabaseName=自己的數(shù)據(jù)庫(kù)名字";
String user="用戶名";
String password="密碼";
Connection conn=null;
try{
Class.forName(sqlDriver).newInstance();
conn=DriverManager.getConnection(url,user,password);
// out.println("數(shù)據(jù)庫(kù)加載成功");
}catch(Exception e){
// out.println("數(shù)據(jù)庫(kù)加載出現(xiàn)錯(cuò)誤!");
}
%
從M$網(wǎng)站下載最新JDBC驅(qū)動(dòng)或都使用maven:
dependency
groupIdcom.microsoft.sqlserver/groupId
artifactIdmssql-jdbc/artifactId
version9.4.1.jre11/version
/dependency
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class SQLDatabaseConnection {
// Connect to your database.
// Replace server name, username, and password with your credentials
public static void main(String[] args) {
String connectionUrl =
"jdbc:sqlserver://yourserver.database.windows.net:1433;"
+ "database=AdventureWorks;"
+ "user=yourusername@yourserver;"
+ "password=yourpassword;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
+ "loginTimeout=30;";
String insertSql = "INSERT INTO SalesLT.Product (Name, ProductNumber, Color, StandardCost, ListPrice, SellStartDate) VALUES "
+ "('NewBike', 'BikeNew', 'Blue', 50, 120, '2016-01-01');";
ResultSet resultSet = null;
try (Connection connection = DriverManager.getConnection(connectionUrl);
PreparedStatement prepsInsertProduct = connection.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS);) {
prepsInsertProduct.execute();
// Retrieve the generated key from the insert.
resultSet = prepsInsertProduct.getGeneratedKeys();
// Print the ID of the inserted row.
while (resultSet.next()) {
System.out.println("Generated: " + resultSet.getString(1));
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
}
}
sqlserver2000與2005的jar文件不一樣。
驅(qū)動(dòng)類,還有連接語(yǔ)句都不一樣。
驅(qū)動(dòng)類:
com.microsoft.jdbc.sqlserver.SQLServerDriver(sqlserver2000)
com.microsoft.sqlserver.jdbc.SQLServerDriver(sqlserver2005)
連接語(yǔ)句:
jdbc:microsoft:sqlserver://server_name:1433 (sqlserver2000)
jdbc:sqlserver://server_name:1433[;databaseName=dbname] (sqlserver2005)
如果都沒(méi)有錯(cuò)可能是
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLSerDriver
中指出的你寫的SQLSerDriver把它改成SQLServerDriver
當(dāng)前名稱:java鏈接sqlserver,java連接sqlserver數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪改查
鏈接分享:http://chinadenli.net/article45/dsedjei.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、Google、定制網(wǎng)站、網(wǎng)站制作、響應(yīng)式網(wǎng)站、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)