1、 安裝時區(qū)別:

專業(yè)領(lǐng)域包括成都做網(wǎng)站、網(wǎng)站建設(shè)、商城系統(tǒng)網(wǎng)站開發(fā)、微信營銷、系統(tǒng)平臺開發(fā), 與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,創(chuàng)新互聯(lián)建站的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。
與mysql相比,Sqlserver安裝后,的配置TCP/IP協(xié)議,Sql Server 身份注冊登錄 ,都比較麻煩。
2、配置連接時區(qū)別:
mysql
使用的驅(qū)動:com.mysql.jdbc.Driver
依賴包:mysql 可以直接通過pom.xml下載
sqlserver驅(qū)動:com.microsoft.sqlserver.jdbc.SQLServerDriver
依賴包:sqljdbc4 需要從本地置入
%
String sqlDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://localhost:1433;DatabaseName=自己的數(shù)據(jù)庫名字";
String user="用戶名";
String password="密碼";
Connection conn=null;
try{
Class.forName(sqlDriver).newInstance();
conn=DriverManager.getConnection(url,user,password);
// out.println("數(shù)據(jù)庫加載成功");
}catch(Exception e){
// out.println("數(shù)據(jù)庫加載出現(xiàn)錯誤!");
}
%
從M$網(wǎng)站下載最新JDBC驅(qū)動或都使用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();
}
}
}
java連接SqlServer2008的數(shù)據(jù)庫連接池使用:
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Vector;
/**數(shù)據(jù)庫連接池的公共類 **/
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";
/**
* 連接池的大小,也就是連接池中有多少個數(shù)據(jù)庫連接
*/
private int poolSize = 5;
private static ConnectionPool instance = null;
/**
* 私有的構(gòu)造方法,禁止外部創(chuàng)建本類的對象,要想獲得本類的對象,通過codegetIstance/code方法
* 使用了設(shè)計模式中的單子模式
*/
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ù)庫連接
*/
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)前連接池的對象
*/
public static ConnectionPool getInstance() {
if (instance == null) {
instance = new ConnectionPool();
}
return instance;
}
/**
* 返回連接池中的一個數(shù)據(jù)庫連接
*/
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ù)庫連接
*/
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();
}
}
}
}
標(biāo)題名稱:java連接sqlserver,java連接sqlserver數(shù)據(jù)庫失敗
URL地址:http://chinadenli.net/article32/dsiohsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站設(shè)計、響應(yīng)式網(wǎng)站、網(wǎng)站策劃、定制開發(fā)、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)