當前位置:首頁 » 數據倉庫 » java跨資料庫訪問資料庫
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

java跨資料庫訪問資料庫

發布時間: 2022-08-20 09:00:25

❶ 如何用java連接資料庫,並且資料庫可以隨java程序移動,從一台機搬到另一台機上也能讀取資料庫

用JDBC連接資料庫,連接好以後,將資料庫備份出來。然後再在另一台機器上還原資料庫(前提另一台機上也要裝有你用的這個資料庫軟體)
連接代碼:這用的是sqlSERVER 2000 SP4
其它的資料庫,你改對應的。 Driver user password url 就可以用了。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Conn {

/**
* @param args
*/

private static Connection con;

private Statement stmt;

private ResultSet rs;

static String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=stms"; //DatabaseName= 這里填上你自己資料庫名字。資料庫用的是SQLSERVER 2000 SP4

static String user = "sa";

static String password = "";

//String sqlStr = "select * from admin";

private static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

public Conn() {

}

public static synchronized Connection getCon() throws Exception {
Class.forName(driver);
System.out.println("成功載入驅動");
try {
con = DriverManager.getConnection(url, user, password);
System.out.println("是否出錯?");
} catch (Exception e) {
;
System.err.println(e.getMessage());
e.printStackTrace();

}
return con;
}

public Statement getstmtread() {
try {
con = getCon();
stmt = con.createStatement(1004, 1007);
return stmt;

} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;

}

public ResultSet getrs(String sql) {

try {

Statement stmt = getstmtread();
rs = stmt.executeQuery(sql);
return rs;

} catch (Exception e) {

System.err.println(e.getMessage());
e.printStackTrace();

}

return null;
}

public Statement getStmt() {

try {
con = getCon();
stmt = con.createStatement();
return stmt;

} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();

}

return null;
}

public synchronized void close() {

if (rs != null) {

try {
rs.close();
rs = null;
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
try {

if (stmt != null)
stmt.close();
stmt = null;

} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}

}
try {
if (con != null)
con.close();
con = null;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}

public synchronized void closeAll() {

if (rs != null) {

try {
rs.close();
rs = null;
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
try {

if (stmt != null)
stmt.close();
stmt = null;

} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}

}
try {
if (con != null)
con.close();
con = null;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}

}

}
//調用 getcon() 方法即可取得連接。 其它方法不多解悉
//別忘了最後關閉資料庫。。

❷ 用Java怎樣訪問資料庫,用什麼代碼

1. 載入一個對應資料庫的JDBC驅動

在建立到一個資料庫的連接之前,必須先載入這個資料庫的JDBC驅動程序,載入之後此driver會自動注冊到JDBC驅動列表中。載入一個JDBC驅動有兩種方法。

a) 在命令行方式下指定驅動器或者用冒號分割驅動器列表:

具體命令如下:

C:\>java –Djdbc.drivers = com.company1.Driver:com.company2.Driver youProject

b)第二種方法,在程序中調用Class.forName()方法。推薦使用。。。。

try

{

String driverName = 「com.imaginary.sql.msql.MsqlDriver」;

Class.forName(driverName).newInstance();

}

Catch(ClassNotFoundException e1)

{

//catch could not find database driver exception.

}

2.連接到資料庫。

根據您後台待連接的資料庫不同,而有小小的差別。

a) 連接到Oracle資料庫。

Connection connection = null ;

try

{

//load the jdbc driver ;

String driverName = 「oracle.jdbc.driver.OracleDriver」;

Class.forName(driverName).newInstance();

//create a connection to the database;

String serverName = 「127.0.0.1」;

String serverPort = 「1521」;

String serverID = 「datebase1」

String userName = 「hello」;

String userPsw = 「world」;

String url = 「jdbc:oracle.thin:@」 + serverName + 「:」 + serverPort + 「:」 + serverID ;

Connection = DriverManager.getConnection(url , userName , userPsw);

}

catch(ClassNotFoundException e1)

{

//catch could not find database driver exception.

}

catch(SQLException e2)

{

//catch could not connect to the database exception.

}

b) 連接到一個SQL Server資料庫。

Connection connection = null ;

try

{

//load the jdbc driver ;

String driverName = 「com.microsoft.jdbc.sqlserver.SQLServerDriver」;

Class.forName(driverName).newInstance();

//create a connection to the database;

String serverName = 「127.0.0.1」;

String serverPort = 「1433」;

String serverID = serverName + serverPort ;

String userName = 「hello」;

String userPsw = 「world」;

String url = 「jdbc:JSQLConnect ://」 + serverID ;

Connection = DriverManager.getConnection(url , userName , userPsw);

}

catch(ClassNotFoundException e1)

{

//catch could not find database driver exception.

}

catch(SQLException e2)

{

//catch could not connect to the database exception.

}

c) 連接到一個MySQL資料庫上。。。。

Connection connection = null ;

try

{

//load the jdbc driver ;

String driverName = 「org.gjt.mm.mysql.Driver」;

Class.forName(driverName).newInstance();

//create a connection to the database;

String serverName = 「127.0.0.1」;

String serverID = 「database」;

String userName = 「hello」;

String userPsw = 「world」;

String url = 「jdbc:mysql ://」 + serverName + 「/」 + serverID ;

Connection = DriverManager.getConnection(url , userName , userPsw);

}

catch(ClassNotFoundException e1)

{

//catch could not find database driver exception.

}

catch(SQLException e2)

{

//catch could not connect to the database exception.

}

綜合上面的三種資料庫連接方式 , 其實大同小異。由於訪問不同的資料庫和所使用的資料庫驅動程序不同,所以導致代碼表面上有小小不同,但透過表面看來,內部都是

1. 載入一個特定的資料庫JDBC驅動。

2. 連接到一個資料庫。

3. 之後,就可以對一個特定的資料庫進行特定的操作了。

附上各種資料庫的JDBC驅動起可用信息網址:

http://java.sun.com/procts/jdbc

對於Oracle資料庫,請參考:

http://otn.oracle.com.software/content.html

對於MySQL資料庫,請參考:

http://mmMySQL.sourceforge.net

對於SQL Server資料庫,有很多的驅動可選,比較常用的:

http://www.microsoft.com/china/sql/downloads/2000/jdbc.asp

http://www.freetds.org

http://www.datadirect-technologies.com

❸ 如何使用Java訪問MySQL資料庫

第1步 獲取Java與MySQL的連接器
第2步 將下載的包存放在自己機器上jdk安裝目錄下面的jre目錄下面的lib目錄下面的ext目錄中,在程序中導入與資料庫操作相關的對象
import java.sql.Connection; //導入資料庫連接對象
import java.sql.DriverManager; //導入資料庫驅動管理對象
import java.sql.ResultSet; //導入數據記錄集對象
import java.sql.SQLException; //導入數據SQL操作異常對象
import java.sql.Statement; //導入SQL操作介面對象
第3步:在程序中寫入如下內容
String url; //連接資料庫的字元串
String sql; //執行數據sql查詢操作的字元串
Connection conn; //資料庫連接變數
Statement st; //資料庫操作對象
ResultSet rs; //數據記錄集對象
url = "jdbc:mysql://localhost:3306/test?user=root&password="; //在程序中只要修改這句,就可以實現資料庫連接
try {
conn = DriverManager.getConnection(url);
st = conn.createStatement();
sql = "select * from test"; //只要修改這句,就可以實現各種查詢操作
rs=st.executeQuery(sql); //執行數據查詢
while(rs.next())
{
System.out.println(rs.getString(1)); //獲得數據表test中第1個欄位的數據,該欄位為字元串類型
System.out.println(rs.getString(2)); //獲得數據表test中第2個欄位的數據,該欄位為字元串類型
}
rs.close(); //關閉數據記錄集
conn.close(); //關閉資料庫連接
} catch (SQLException e) {
System.out.println("Error:"+e.toString()+e.getMessage());
}

❹ 如何在Java程序中訪問mysql資料庫中的數據並進行簡單的操作

一、使用工具:java語言、Myeclipse。二、操作步驟:1、第一步:載入MySQL的JDBC的驅動2、第二步:創建與MySQL資料庫的連接類的實例3、第三步:獲取連接類實例con,用con創建Statement對象類實例sql_statement4、第四步:執行查詢,用Resu

❺ java怎麼連異地資料庫

"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";

只用把localhost換成目標機器的地址就可以啦,比如區域網的IP就可以啦.

還可以用遠程調用,來訪問遠程資料庫. (RMI);

❻ JAVA中如何同時訪問兩種資料庫

一般是在配置文件中寫資料庫連接的信息,你可以寫兩種不同資料庫(如Oracle和SQL Server)的數據源,然後用程序讀入不同的配置文件,接下來就可以分別訪問兩種不同的資料庫了,但不是同時的

如果兩個資料庫是同一種資料庫中的不同資料庫,如在MySQL中建立了兩個資料庫A和B,那麼可以直接用代碼進行條件處理資料庫操作就可以了

❼ 用java訪問資料庫要用到哪些知識

主要是JDBC,詳細了解其中的API如果不想自己設計資料庫層,那麼去了解Hibernet,這是資料庫層的框架,很好用另外,訪問資料庫,還需要了解
SQL語句