java连接oracle出现问题
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)
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;
}
}
}
}
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:**";
url的sid错了,他表示你所要连接的数据库名字,你看看你型兆山要连接的数据库是不是“**”,若不是你找卜中一个你经常用的猜汪数据库试试
你把URL中的localhost改成127.0.0.1试试
然后确定你的服务中oracle的此塌服敬扒帆务以亮雹及监听启动没,
"jdbc:oracle:thin:@localhost:1521:**";
**对应你数孝空据库巧咐瞎安装时简卖填的名字
net start oracleservice++ 对应++