java连接oracle出现问题
packageclassexcs;importjava.sql.*;publicclassLab1{publicstaticvoidmain(String[]args){...
package classexcs;
import java.sql.*;
public class Lab1
{
public static void main(String[] args){
Connection con = null;
try{
//1.注册驱动
String driverClassName=
"oracle.jdbc.driver.OracleDriver";
Class.forName(driverClassName);
//2.连接数据库
String url =
"jdbc:oracle:thin:@localhost:1521:**";
String userName="system";
String pwd="**";
con = DriverManager.getConnection(
url,userName,pwd);
System.out.println("con->"+con);
}catch(Exception e){
e.printStackTrace();
}finally{
//关闭连接
try{
if(con!=null) con.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
错误为:
java.sql.SQLException: Io 异常: Invalid connection string format, a valid format is: "host:port:sid"
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:274)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:319)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:344)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:148)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:545)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at classexcs.JdbcUtil.getConnection(JdbcUtil.java:24)
at classexcs.Lab8.main(Lab8.java:15)
java.lang.NullPointerException
at classexcs.Lab8.main(Lab8.java:17) 展开
import java.sql.*;
public class Lab1
{
public static void main(String[] args){
Connection con = null;
try{
//1.注册驱动
String driverClassName=
"oracle.jdbc.driver.OracleDriver";
Class.forName(driverClassName);
//2.连接数据库
String url =
"jdbc:oracle:thin:@localhost:1521:**";
String userName="system";
String pwd="**";
con = DriverManager.getConnection(
url,userName,pwd);
System.out.println("con->"+con);
}catch(Exception e){
e.printStackTrace();
}finally{
//关闭连接
try{
if(con!=null) con.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
错误为:
java.sql.SQLException: Io 异常: Invalid connection string format, a valid format is: "host:port:sid"
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:274)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:319)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:344)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:148)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:545)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at classexcs.JdbcUtil.getConnection(JdbcUtil.java:24)
at classexcs.Lab8.main(Lab8.java:15)
java.lang.NullPointerException
at classexcs.Lab8.main(Lab8.java:17) 展开
展开全部
sid写错了,url中的JCMHHIS是sid,你的**是什么?
看看我的
import java.sql.*;
public class JDBConnection {
private final String dbDriver =
"oracle.jdbc.driver.OracleDriver";
private final String url =
"jdbc:oracle:thin:@10.10.40.2:1521:JCMHHIS";
private final String userName = "username";
private final String password = "password";
private Connection con = null;
public JDBConnection() {
try {
Class.forName(dbDriver).newInstance();
} catch (Exception ex) {
System.out.println("couldn't find driver");
}
}
public boolean creatConnection() {
try {
con = DriverManager.getConnection(url, userName, password);
con.setAutoCommit(true);
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("creatConnectionError!");
}
return true;
}
public boolean executeUpdate(String sql) {
if (con == null) {
creatConnection();
}
try {
Statement stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println("update" + String.valueOf(iCount));
return true;
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("executeUpdaterError!");
return false;
}
}
public ResultSet executeQuery(String sql) {
ResultSet rs;
try {
if (con == null) {
creatConnection();
}
Statement stmt = con.createStatement();
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
return null;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("executeQueryError!");
return null;
}
return rs;
}
public void closeConnection() {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Failed to close connection!");
} finally {
con = null;
}
}
}
}
看看我的
import java.sql.*;
public class JDBConnection {
private final String dbDriver =
"oracle.jdbc.driver.OracleDriver";
private final String url =
"jdbc:oracle:thin:@10.10.40.2:1521:JCMHHIS";
private final String userName = "username";
private final String password = "password";
private Connection con = null;
public JDBConnection() {
try {
Class.forName(dbDriver).newInstance();
} catch (Exception ex) {
System.out.println("couldn't find driver");
}
}
public boolean creatConnection() {
try {
con = DriverManager.getConnection(url, userName, password);
con.setAutoCommit(true);
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("creatConnectionError!");
}
return true;
}
public boolean executeUpdate(String sql) {
if (con == null) {
creatConnection();
}
try {
Statement stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println("update" + String.valueOf(iCount));
return true;
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("executeUpdaterError!");
return false;
}
}
public ResultSet executeQuery(String sql) {
ResultSet rs;
try {
if (con == null) {
creatConnection();
}
Statement stmt = con.createStatement();
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
return null;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("executeQueryError!");
return null;
}
return rs;
}
public void closeConnection() {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Failed to close connection!");
} finally {
con = null;
}
}
}
}
展开全部
jdbc:oracle:thin:@dlsun511:1521:ora1
String url =
"jdbc:oracle:thin:@localhost:1521:**";
修改为
String url =
"jdbc:oracle:thin:@127.0.0.1:1521:**";
原因是你用oracle版本是10,所以不能用localhost来表示计算机,不过如果你的计算机名字为baidu那么还可以这样写
String url =
"jdbc:oracle:thin:@baidu:1521:**";
String url =
"jdbc:oracle:thin:@localhost:1521:**";
修改为
String url =
"jdbc:oracle:thin:@127.0.0.1:1521:**";
原因是你用oracle版本是10,所以不能用localhost来表示计算机,不过如果你的计算机名字为baidu那么还可以这样写
String url =
"jdbc:oracle:thin:@baidu:1521:**";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
url的sid错了,他表示你所要连接的数据库名字,你看看你要连接的数据库是不是“**”,若不是你找一个你经常用的数据库试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把URL中的localhost改成127.0.0.1试试
然后确定你的服务中oracle的服务以及监听启动没,
然后确定你的服务中oracle的服务以及监听启动没,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
"jdbc:oracle:thin:@localhost:1521:**";
**对应你数据库安装时填的名字
net start oracleservice++ 对应++
**对应你数据库安装时填的名字
net start oracleservice++ 对应++
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询