java连接oracle出现问题

2024-12-18 10:51:55
推荐回答(5个)
回答1:

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;
}
}
}

}

回答2:

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:**";

回答3:

url的sid错了,他表示你所要连接的数据库名字,你看看你要连接的数据库是不是“**”,若不是你找一个你经常用的数据库试试

回答4:

你把URL中的localhost改成127.0.0.1试试
然后确定你的服务中oracle的服务以及监听启动没,

回答5:

"jdbc:oracle:thin:@localhost:1521:**";
**对应你数据库安装时填的名字
net start oracleservice++ 对应++