我試圖建立從Java到Oracle DB的連接。 (我的數據庫在另一臺機器上)JDBC連接錯誤到Oracle數據庫
我所知道的URL的形式如下所示:String url =「jdbc:oracle:thin:@hostname:portnumber:sid」;
這裏是我的Java代碼建立連接:
package net.metric.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DemoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text");
PrintWriter out = response.getWriter();
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//CONNECT TO DB
String url = "jdbc:oracle:thin:@252.112.60.47:1521:XE";
System.out.println(url);
Connection conn = DriverManager.getConnection(url,"EXT025","Tellcom30");
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
System.out.println("OK");
/* ResultSet rset =
stmt.executeQuery("select * from SBO_AUDIT_NEW.AUDIT_EVENT");
while (rset.next()) {
System.out.println (rset.getString(1));
}
stmt.close();
System.out.println ("Ok.");*/
}catch(Exception e){
System.out.println(e.getMessage());
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
我得到這個錯誤:
-------- Oracle JDBC Connection Testing ------
Io exception: The Network Adapter could not establish the connection
我在做什麼錯?任何答案將不勝感激。 謝謝
你確認你的oracle數據庫設置爲接受本地主機之外的連接嗎? – abalos 2014-09-02 12:15:41
我希望您的示例中的用戶名和密碼是假的:-) – Leo 2014-09-02 12:16:10
ping您遠程主機,並檢查它是否接受遠程連接,以及爲什麼您需要註冊驅動程序兩次? – SparkOn 2014-09-02 12:16:29