在JAVA中怎样连接数据库? 运行环境: window2000+Jcreate pro2.5+mdac2.7 数据库在服务器上,不在我机器上 数据库我有权限,在DELPHI下面很正常 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 顺便加一句:我的JDK是1。4版本的,我在JBUILDER中使用DATABASE控件完全可以连上。上面的那位,你要注意出错的是语法方面的错误,和权限没关系 System.setProperty ("jdbc.drivers",drivers);中间多了个" "!你试试!System.setProperty("jdbc.drivers",drivers); 用下列语句试试: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //打开数据库连接 Connection connection1= DriverManager.getConnection(url,username,password);另,我做了一个专门用于连接数据库的类,你可以看看(连接数据库绝对没有问题):/*使用该对象可以自动连接到支持JDBC的数据 库,从而方便的进行数据库操作 注:DBURL(数据源) 举例: jdbc:odbc:gisdb dbRun(数据库驱动程序) 举例: com.ms.jdbc.odbc.JdbcOdbcDriver 程序:朱林 2001.4.21*/package zhulin.db;import java.sql.*;import java.util.*;public class DataBase{ private Statement s; private String DBURL; //数据源 private String dbRun; //数据库所用驱动程序 private Connection con; public DataBase(String DBURL,String dbRun,String user,String password) throws Exception{ this.DBURL =DBURL; this.dbRun =dbRun; this.connectDB (user,password); } //数据库系列操作 //连接到SQL数据库 public synchronized void connectDB(String user,String password) throws java.sql.SQLException ,java.lang.NullPointerException, java.lang.ClassNotFoundException{ //装载jdbc驱动程序 System.out.println("Use \""+user+"\"Connects the DataBase..."); Class.forName(dbRun); //打开数据库连接 con = DriverManager.getConnection(DBURL,user,password); s =con.createStatement(); //创建SQL语句执行对象 System.out.println("The DataBase has Connected Successfully!"); } //关闭数据库 public void closeDB() throws java.sql.SQLException , java.lang.NullPointerException{ s.close(); s=null; } public Statement getStatement(){ return this.s; } public Connection getConnection(){ return this.con; }} private boolean connect(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); String url="jdbc:odbc:boyun"; String user="boyun"; String password="boyun"; conn= DriverManager.getConnection(url,user,password); stmt=conn.createStatement(); bConnected = true; } catch(Exception ee) { if (this.bDebugFlag) { this.sErrMeg+=("connect sql:"+ee.getMessage())+"<br>"; } return false; } return true; } java与JNI的问题,我哪里有错? java or oracle 技术群 怎么求两个日期相差几个月,几个星期,几年? 请帮助解决一下谢谢(初学者,万分感谢)很急!! String是否是基本数据类型? 没有心情,散分! 2 邮件乱码问题,急!!! web服务如何向指定页面推送数据 exception:com.ms.security.securityException[host]:cannot access "202.113.189.115":1111? 一个一直不太理解的问题! 向各位请教 关于hashcode()
你试试!
System.setProperty("jdbc.drivers",drivers);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//打开数据库连接
Connection connection1= DriverManager.getConnection(url,username,password);另,我做了一个专门用于连接数据库的类,你可以看看(连接数据库绝对没有问题):
/*使用该对象可以自动连接到支持JDBC的数据
库,从而方便的进行数据库操作
注:DBURL(数据源) 举例:
jdbc:odbc:gisdb
dbRun(数据库驱动程序) 举例:
com.ms.jdbc.odbc.JdbcOdbcDriver
程序:朱林 2001.4.21
*/
package zhulin.db;
import java.sql.*;
import java.util.*;
public class DataBase{
private Statement s;
private String DBURL; //数据源
private String dbRun; //数据库所用驱动程序
private Connection con;
public DataBase(String DBURL,String dbRun,String user,String password)
throws Exception{
this.DBURL =DBURL;
this.dbRun =dbRun;
this.connectDB (user,password);
}
//数据库系列操作
//连接到SQL数据库
public synchronized void connectDB(String user,String password)
throws java.sql.SQLException ,java.lang.NullPointerException,
java.lang.ClassNotFoundException{
//装载jdbc驱动程序
System.out.println("Use \""+user+"\"Connects the DataBase...");
Class.forName(dbRun);
//打开数据库连接
con = DriverManager.getConnection(DBURL,user,password);
s =con.createStatement(); //创建SQL语句执行对象
System.out.println("The DataBase has Connected Successfully!");
}
//关闭数据库
public void closeDB() throws java.sql.SQLException ,
java.lang.NullPointerException{
s.close();
s=null;
}
public Statement getStatement(){
return this.s;
}
public Connection getConnection(){
return this.con;
}
}
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String url="jdbc:odbc:boyun";
String user="boyun";
String password="boyun";
conn= DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
bConnected = true;
}
catch(Exception ee)
{
if (this.bDebugFlag)
{
this.sErrMeg+=("connect sql:"+ee.getMessage())+"<br>";
}
return false;
}
return true;
}