当前位置: 技术问答>java相关
jdbc连接oracle的问题?大家请进
来源: 互联网 发布时间:2015-05-01
本文导语: 我写了如下的连接oracle8i的类,但编译通不过,请指点: import java.sql.*; public class Ora8iConnect { private Connection conn; //连接对象 private String dbNAME; //实例 private String userID; //用户名 private String userPWD; //口令 ...
我写了如下的连接oracle8i的类,但编译通不过,请指点:
import java.sql.*;
public class Ora8iConnect
{
private Connection conn; //连接对象
private String dbNAME; //实例
private String userID; //用户名
private String userPWD; //口令
public Ora8iConnect(String db,String id,String pwd) //constructor
{ dbNAME=db;
userID=id;
userPWD=pwd;
beginConnect(); //连接数据库
}
/*-
*返回一个Connection对象
*/
public Connection getConnection(){return conn;}
/*-
*连接数据库,成功后返回1否则返回0
*/
public int beginConnect()
{ try
{ //加载一个Oracle驱动
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//使用OCI8连接到数据库
conn=DriverManager.getConnection("jdbc:oracle:oci8:@"+dbNAME,userID,userPWD);
return 1;
}
catch(SQLException e) //捕捉SQL违例
{ System.out.println("Ora8iConnect在连接oracle8数据库时捕获");
while (e!=null)
{ System.out.println("SQLState:"+e.getSQLState());
System.out.println("Message :"+e.getMessage());
System.out.println("Vendor :"+e.getErrorCode());
e=e.getNextException();
System.out.println(" ");
}
conn=null;
return 0;
}
}
}
编译错误行为:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
是不是我还需装点什么驱动?
import java.sql.*;
public class Ora8iConnect
{
private Connection conn; //连接对象
private String dbNAME; //实例
private String userID; //用户名
private String userPWD; //口令
public Ora8iConnect(String db,String id,String pwd) //constructor
{ dbNAME=db;
userID=id;
userPWD=pwd;
beginConnect(); //连接数据库
}
/*-
*返回一个Connection对象
*/
public Connection getConnection(){return conn;}
/*-
*连接数据库,成功后返回1否则返回0
*/
public int beginConnect()
{ try
{ //加载一个Oracle驱动
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//使用OCI8连接到数据库
conn=DriverManager.getConnection("jdbc:oracle:oci8:@"+dbNAME,userID,userPWD);
return 1;
}
catch(SQLException e) //捕捉SQL违例
{ System.out.println("Ora8iConnect在连接oracle8数据库时捕获");
while (e!=null)
{ System.out.println("SQLState:"+e.getSQLState());
System.out.println("Message :"+e.getMessage());
System.out.println("Vendor :"+e.getErrorCode());
e=e.getNextException();
System.out.println(" ");
}
conn=null;
return 0;
}
}
}
编译错误行为:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
是不是我还需装点什么驱动?
|
对,把ORACLE的驱动加到CLASSPATH中
|
把../oracle/ora81/jdbc/lib/*.jar或.zip,加入classpath中即可