java連接遠程資料庫
㈠ java jConnect-6_0連接遠程sybase資料庫
1、看看文件名是不是 ConnTest ,文件名要和方法名匹配。
2、把第一行:import com.sybase.jdbc3.jdbc.SybDriver; 刪掉,把工作環境切換到你的文件夾下。
3、連接
先安裝sybase open client並設置資料庫連接,再用如下代碼
<%
dim ConnStr,connoss
ConnStr = "Driver={SYBASE SYSTEM 11};srvr=***;database=***;uid=***;pwd=***;"
Set connoss = Server.CreateObject("ADODB.Connection")
connoss.open ConnStr
sub closeconnoss()
connoss.close
set connoss=nothing
end sub
If Err Then
err.Clear
Set Connoss = Nothing
Response.Write "CONNECT TO OSSDB Error"
Response.End
End If
%>
㈡ 如何用java 遠程連接 access資料庫
package com.jiuzi.connection;
import java.sql.*;
public class ConnectAccess {
/**
* 初學者請注意:
* 1:先建立一個access文件a1.mdb,並放在D:\下;
* 2:在資料庫文件a1.mdb中建立一個表Table1;
* 3:為Table1添加一列,並插入至少一條記錄;
* 4:本文是一個完整的類,直接拿去運行就可以。
*/
public static void main(String args[]) throws Exception {
ConnectAccess ca=new ConnectAccess();
ca.ConnectAccessFile();
// ca.ConnectAccessDataSource();
}
public void ConnectAccessFile() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/**
* 直接連接access文件。
*/
String dbur1 = "jdbc:odbc:driver={Microsoft Access Driver
(*.mdb)};DBQ=d:\\hongloumeng.mdb";
Connection conn = DriverManager.getConnection(dbur1, "username", "password");
Statement stmt = conn.createStatement();
String sql="select * from dream where 序號=1";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String text=rs.getString(4);
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(text);
}
rs.close();
stmt.close();
conn.close();
}
public void ConnectAccessDataSource()throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/**
* 採用ODBC連接方式 如何建立ODBC連接?
* 答:在windows下,【開始】->【控制面板】->【性能和維護】->【管理工具】->【數
據源】,在數據源這里添加一個指向a1.mdb文件的數據源。
* 比如創建名字為dataS1
*/
String dbur1 = "jdbc:odbc:dataS1";// 此為ODBC連接方式
Connection conn = DriverManager.getConnection(dbur1, "username", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from Table1");
while (rs.next()) {System.out.println(rs.getString(1));
}
rs.close();
stmt.close();
conn.close();
}
}
利用連接池(以下是一個連接池)
package com.jiuzi.connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class ConnectionPool {
private List<ConnectionDesc> connections = new ArrayList<ConnectionDesc>();
private static final int MIN_CONNECTIONS = 2;
private static final int MAX_CONNECTIONS = 10;
private static ConnectionPool connectionPool = null;
static {
try {
connectionPool = new ConnectionPool();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "資料庫連接錯誤:"+
("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:\\hongloumeng.mdb"+
e.getMessage()));
System.exit(-1);
System.err.println();
}
}
private ConnectionPool() throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
for(int i = 0; i < MIN_CONNECTIONS; ++i) {
this.createNewConnection();
}
}
public static ConnectionPool getInstance() {
return connectionPool;
}
public static void main(String [] args) throws Exception {
final ConnectionPool pool = ConnectionPool.getInstance();
for(int i = 0; i < 5; ++i) {
new Thread() {
public void run() {try {
Connection conn = pool.getConnection();
Thread.sleep(5000);
pool.releaseConnection(conn);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
public synchronized Connection getConnection() throws Exception {
ConnectionDesc cd = null;
for (int i = 0; i < connections.size(); i++) {
cd = connections.get(i);
if (cd.isIdle()) {
cd.setState(true);
System.out.println("Connection No." + cd.getNo() + " has been
Occupied.");
return cd.getConn();
}
}
cd = createNewConnection();
cd.setState(true);
return cd.getConn();
}
public synchronized void releaseConnection(Connection conn) {
for (int i = 0; i < this.connections.size(); i++) {
ConnectionDesc cd = this.connections.get(i);
if (cd.getConn() == conn) {
cd.setState(false);
System.out.println("Connection No." + cd.getNo() + " has been
released.");
}
}
}
public synchronized ConnectionDesc createNewConnection()
throws Exception {
if (this.connections.size() < MAX_CONNECTIONS) {
Connection conn =
DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver
(*.mdb)};DBQ=db\\hongloumeng.mdb",
"username", "password");
ConnectionDesc cd = new ConnectionDesc(conn);
this.connections.add(cd);
cd.setState(false);
return cd;
}
System.err.println("Too many Connection with DB:" + connections.size());
if(JOptionPane.OK_OPTION==JOptionPane.showConfirmDialog(null, "E00101:太多連接
了,對其連接復位嗎?")){
resetConnection();
}
return null;
}
public void resetConnection(){
for(ConnectionDesc desc:connections){
if(!desc.isIdle()){
desc.setState(false);
}
}
}
}
class ConnectionDesc {
private Connection conn;
//state 指的是此連接是否被使用: true 為是,false 為不是
private boolean state;
private int no;
private static int count = 0;
ConnectionDesc(Connection conn) {
this.conn = conn;
this.no = count++;
System.out.println("Connection No." + this.no + " has been created.");
}
public void setState(boolean state) {
this.state = state;
}
public boolean isIdle() {
return !state;
}
public int getNo() {
return this.no;
}
public Connection getConn() {
return conn;
}
}
㈢ Java實現訪問遠程資料庫的實例
Mysql默認關閉遠程登錄許可權,需要給遠程用戶添加許可權才能連接。
通過select*from mysql.user\G 查看用戶及許可權。
一.允許root用戶在任何地方進行遠程登錄,並具有所有庫任何操作許可權,具體操作如下:
1、在本機先使用root用戶登錄mysql:
mysql -u root -p"password"
2、進行授權操作:
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
3、重載授權表:flush privileges;
二.允許root用戶在一個特定的IP進行遠程登錄,並具有所有庫任何操作許可權,具體操作如下:
1、在本機先使用root用戶登錄mysql:
mysql -u root -p"password"
2、進行授權操作:
GRANT ALL PRIVILEGES ON *.* TO root@"172.16.16.152" IDENTIFIED BY "password" WITH GRANT OPTION;
3、重載授權表:flush privileges;