当前位置:首页 » 操作系统 » java连接远程数据库

java连接远程数据库

发布时间: 2023-11-05 07:56:20

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;

热点内容
四川密码门锁在哪里买 发布:2024-11-30 12:50:29 浏览:400
手机编写脚本软件 发布:2024-11-30 12:41:02 浏览:872
学php如何 发布:2024-11-30 12:36:48 浏览:857
家庭电脑改网盘服务器 发布:2024-11-30 12:32:13 浏览:105
电脑服务器组装系统 发布:2024-11-30 12:32:11 浏览:998
在线显示wifi密码是什么 发布:2024-11-30 12:30:53 浏览:726
触动精灵安卓脚本 发布:2024-11-30 12:30:47 浏览:80
phpmd5加密代码 发布:2024-11-30 12:29:35 浏览:899
苹果手机锁屏怎么取消密码 发布:2024-11-30 12:28:08 浏览:860
老娘也要当间谍ftp 发布:2024-11-30 12:23:17 浏览:131