當前位置:首頁 » 編程語言 » java訪問sqlserver
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

java訪問sqlserver

發布時間: 2022-08-03 04:06:39

❶ java連接sqlserver資料庫問題

你連接字元串寫錯了,我給你一個我原來寫的,我使用的是SQL2005資料庫
import java.sql.*;
public class BaseDAO {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String CONNECTION = "jdbc:sqlserver://localhost:1433;databaseName=Employee";
private static final String NAME = "sa";
private static final String PWD = "sasa";
public static Connection GetConnection() {
Connection con = null;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(CONNECTION, NAME, PWD);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return con;
}
public static void close(ResultSet rs, PreparedStatement ps, Connection con) {
try {
if (null != rs) {
rs.close();
}
if (null != ps) {
ps.close();
}
if (null != con) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

❷ Java連接SqlServer的最常用的方法是哪兩種

一般是使用jdbc-odbc橋連接執行方式有以下兩種格式:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")//用於載入JDBC-ODBC橋驅動程序;
格式一:執行基本SQL語句
Class.forName("JDBC驅動程序");
Connection conn=DriverManager.getConnection("相應JDBC驅動程序的連接串);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(「SQL語句");//如果是數據查詢
stmt.executeUpdate("SQL語句");//如果是數據操作

格式二:執行帶參數的SQL語句
Class.forName("JDBC驅動程序");
Connection conn=DriverManager.getConnection("相應JDBC驅動程序的連接串);
PreparedStatement stmt = connect.PreparedStatement (帶參數的SQL語句);
stmt.setInt(設置參數的值);
ResultSet rs = stmt.executeQuery(); //如果是數據查詢
stmt.executeUpdate(); //如果是數據操作

❸ JAVA如何連接到sqlserver

你這個程序不是從資料庫查詢,是通過讀取gong1.txt文件判斷查詢的啊

如果你要改成從資料庫里查詢要把以下代碼替換掉:
FileInputStream come_in42=new FileInputStream("gong1.txt");
ObjectInputStream in42 =new ObjectInputStream(come_in42);
list=(LinkedList)in42.readObject();
in42.close();

替換為:
Connection databaseConnect = null; // 資料庫連接
Statement sqlServerStmt = null;
ResultSet sqlServerRset = null;
Statement ps = null;
String localDatabaseDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String localDatabaseUrl =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=qs080521";//註:這里要寫你要連接的資料庫,把(qs080521)替換掉
try {
Class.forName(localDatabaseDriver);
databaseConnect = DriverManager.getConnection(localDatabaseUrl, "sa", "");
String DanJuHao = "";
sqlServerStmt = databaseConnect.createStatement();

String sqlStr = "";//寫SQL查詢語句
System.out.println(sqlStr);

sqlServerRset = ps.executeQuery(sqlStr);
while(sqlServerRset.next()){
Wage w = new Wage();
//用sqlServerRset.get...() 方法取出對應的數值
//w.set...();將上面語句放到括弧內,存儲到相應欄位
list.add(w);
}
sqlServerRset.close();
databaseConnect.close();

下面就什麼也不用改了

❹ 如何通過JAVA操作SQLserver資料庫

之前遠標教育老師教我們這樣把sqlserver jdbc驅動加到classpath中,三個jar包。
import java.sql.*;
public class DbTest
{
Connection con;
Statement sta;
ResultSet rs;
String driver;
String url;
String user;
String pwd;
public DbTest()
{
driver = "sun.jdbc.odbc.JdbcOdbcDriver";
url = "jdbc:odbc:store_manager";
user = "share";
pwd = "share";
init();
}
public void init()
{
try{
rName(driver);
intln("driver is ok");
con = tConnection(url,user,pwd);
intln("conection is ok");
sta = eateStatement();
rs = sta.executeQuery("select * from room");
while( xt())
intln( tInt("roomNum"));
}
catch(Exception e)
{
intStackTrace();
}
}
public static void main(String[] args)
{
new DbTest();
}
}

❺ JAVA編程實現訪問SQL Server資料庫

java連接SQL Server2000
(現在企業最常用的資料庫就是SQL Server2000,所以只說這個,別的都是大同小異,連接access,mysql,Oracle資料庫遇到問題的可以和我聯系)
java連接資料庫一般有兩種方式,一是通過jdbc/odbc橋(需要配置數據源),二是通過jdbc驅動.這里強烈推薦使用後者,因為前者涉及到jdbc到odbc轉換的問題,執行效率很低.
首先下載jdbc驅動包(用搜索引擎搜下,多如牛毛),安裝之後,打開那個文件夾,會看到三個jar包,只要把這三個jar包設置到環境變數(classpath)里就可以了
假設驅動程序安裝在d:\sqldriver目錄下,那麼就在classpath中添加d:\sqldriver\msbase.jar;d:\sqldriver\mssqlserver.jar;d:\sqldriver\msutil.jar;
注意:如果使用的是windows xp系統的sp2 版本,就需要給SQL Server2000打一個sp3a或者sp4的補丁.打完之補丁之後才可以使用
測試代碼(使用的是SQL Server自帶的資料庫,可以直接編譯運行)
import java.sql.*;
public class SqlTesting {
public static void main(String args[]) {
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";
String user ="sa";
String password = "sa";
String sqlStr = "select CustomerID, CompanyName, ContactName from Customers";

try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println( "" );

Connection con = DriverManager.getConnection( url, user, password );
Statement st = con.createStatement();
ResultSet rs = st.executeQuery( sqlStr );

while(rs.next()) {
System.out.print(rs.getString("CustomerID") + " ");
System.out.print(rs.getString("CompanyName") + " ");
System.out.println(rs.getString("ContactName"));
}
rs.close();
st.close();
con.close();
} catch(Exception err) {
err.printStackTrace(System.out);
}
}
}

❻ java怎麼連接sqlserver資料庫

java中使用jdbc連接sql server資料庫步驟:
1.JDBC連接SQL Server的驅動安裝 ,前兩個是屬於資料庫軟體,正常安裝即可(注意資料庫登陸不要使用windows驗證)
<1> 將JDBC解壓縮到任意位置,比如解壓到C盤program files下面,並在安裝目錄里找到sqljdbc.jar文件,得到其路徑開始配置環境變數
在環境變數classpath 後面追加 C:\Program Files\Microsoft SQL Server2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar
<2> 設置SQLEXPRESS伺服器:
a.打開SQL Server Configuration Manager -> SQLEXPRESS的協議 -> TCP/IP
b.右鍵單擊啟動TCP/IP
c.雙擊進入屬性,把IP地址中的IP all中的TCP埠設置為1433
d.重新啟動SQL Server 2005服務中的SQLEXPRESS伺服器
e.關閉SQL Server Configuration Manager
<3> 打開 SQL Server Management Studio,連接SQLEXPRESS伺服器, 新建資料庫,起名字為sample
<4> 打開Eclipse
a.新建工程-> Java -> Java project,起名為Test
b.選擇eclipse->窗口->首選項->Java->installed JRE 編輯已經安裝好的jdk,查找目錄添加sqljdbc.jar
c.右鍵單擊目錄窗口中的Test, 選擇Build Path ->Configure Build Path..., 添加擴展jar文件,即把sqljdbc.jar添加到其中
<5> 編寫Java代碼來測試JDBC連接SQL Server資料庫
import java.sql.*;
public class Test {
public static void main(String[] srg) {
//載入JDBC驅動
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//連接伺服器和資料庫sample
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=sample";
String userName = "sa"; //默認用戶名
String userPwd = "123456"; //密碼

Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); //如果連接成功 控制台輸出
} catch (Exception e) {
e.printStackTrace();
}
}
}

執行以後就可以連接到sample資料庫了。

❼ JAVA連接SQL資料庫

本文將介紹使用java連接sqlserver資料庫


工具/材料

myeclipse 、 SqlServer資料庫


方法:

1、要向連接資料庫,首先應該保證資料庫服務打開

2、資料庫服務打開之後就可以在環境中編寫連接代碼了。如圖:


連接資料庫就是這兩個步驟:1)載入驅動、2)創建連接。

注意在導包是導入的java.sql下的。

接下來直接運行一下就可以測試是否連接成功了

❽ java如何訪問遠程的sqlserver2008

  1. 確定你sqlserver開通了 tcp/ip服務;//這個最重要!!!!!!!!!!!!!!!!!!!!!!!!!!!

  2. 驅動包的官方下載:http://www.microsoft.com/zh-cn/download/details.aspx?id=2505

  3. Class.forname("com.microsoft.jdbc.sqlserver.SQLServerDriver");

  4. Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:你設置的埠號,一般是1433;DatabaseName=資料庫名", "資料庫用戶名","資料庫密碼");

  5. 祝你成功

下面是如何開啟tcp/ip的

http://www.2cto.com/database/201304/204867.html

❾ java如何連接SQLserver資料庫

注意:在使用這個類的時候,先將對應資料庫的驅動包(JAR包),復制進項目的WebRoot文件夾下的WEB-INF文件夾下的lib文件夾下,切記必須要對應的JAR包,否則無法使用資料庫的
import java.sql.*;
public class BaseDAO {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//注意:此驅動是SQL2005及以上版本的導入驅動包連接字元串
private static final String CONNECTION = "jdbc:sqlserver://localhost:1433;databaseName=Employee"; //資料庫連接字元串,databaseName就是你要連接的資料庫名,
private static final String NAME = "sa"; //資料庫用戶名
private static final String PWD = "sa"; //資料庫密碼
public static Connection GetConnection() {
Connection con = null;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(CONNECTION, NAME, PWD);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return con;
}

public static void close(ResultSet rs, PreparedStatement ps, Connection con) {
try {
if (null != rs) {
rs.close();
}
if (null != ps) {
ps.close();
}
if (null != con) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

❿ 怎麼用java連接sqlserver資料庫

  1. 導入SqlServer JDBC的驅動,

  2. SQLServer的JDBC URL=

jdbc:sqlserver://172.30.202.21:1433;DatabaseName=AirAutoMonitor

3. 獲得連接的代碼

(Stringurl,Stringusername,Stringpassword)
{
Connectionconn=null;
StringdriverName="";

Propertiesprops=newProperties();
props.put("user",username);
props.put("password",password);

if(url!=null||!"".equals(url)){
if(url.indexOf("oracle")>-1){
databaseType="oracle";
props.put("remarksReporting","true");
driverName="oracle.jdbc.driver.OracleDriver";
}
if(url.indexOf("sqlserver")>-1){
databaseType="sqlserver";
driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
}
if(url.indexOf("mysql")>-1){
databaseType="mysql";
driverName="com.mysql.jdbc.Driver";
}
}
try{
Class.forName(driverName);
conn=DriverManager.getConnection(url,props);
}catch(ClassNotFoundExceptione){
(e);
}catch(SQLExceptione){
(e);
}
returnconn;
}

上面的代碼是獲得Oracle, MySQL, SqlServer的資料庫連接的通用方法。