这是程序代码:
package xtu.tao.mrg;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConn {
private Connection conn = null;
private Statement st = null;
private ResultSet rs = null;
public Connection getConnection()//获得对数据库的连接
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");//查找驱动类
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1527:TAO8","cheng","6852577");
//建立连接套接字
//数据库类型标识: jdbc:oracle:thin:
//数据库服务器IP和端口: @127.0.0.1:1527:
//全局数据库名: test;
//后两个参数分别为你所创建的用户名和密码
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();//可能存在的异常
}
catch(SQLException e)
{
e.printStackTrace();
}
if(conn!=null)
{
System.out.println("连接成功");
}
return conn;
}
public void closeDB()//关闭数据库连接,这一步一定不能少
{
if(conn!=null)//判断连接是否存在,存在就关闭
{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String args[])
{
new DBConn().getConnection();
}
}
这是报的错误:
java.sql.SQLException: Io 异常: Unexpected packet
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:180)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:222)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:335)
at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:361)
at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:442)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:321)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at xtu.tao.mrg.DBConn.getConnection(DBConn.java:18)
at xtu.tao.mrg.DBConn.main(DBConn.java:51)
加载的驱动是:classes12.jar.