English 中文(简体)
我用java无法连接到数据库
原标题:I can t connect to my database using java
Connection conn = null;
Statement stmt=null;
ResultSet rset=null;
String jdbc_url="jdbc:oracle:thin:hr/hr@bassam-desktop:1521:XE";
String query="";
try
{
    DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
    conn=DriverManager.getConnection(jdbc_url);
    stmt=(Statement) conn.createStatement();
    query="select first_name" +"from employees"+"where employeed_id=100";
    rset=stmt.executeQuery(query);
    System.out.println(rset.getString(1));


}
catch(SQLException | NumberFormatException e)
        {
            System.out.println("result error");
        }
finally{
    try{
        rset.close();
        stmt.close();
        conn.close();

    }
    catch(Exception e)
    {
        System.out.println("Error in closing");
    }
}

我得到两个错误 在结果窗口,有什么问题? hr 密码是正确的, 我的主机名是正确的, 是不是它从您右键点击窗口 xp 中的“ 我的电脑” 后从“ 计算机名称” 获得的?

编辑: 使用 e.getMessage () 后, 我得到了这个...

结果错误, 结果Set. next 不被称为

最佳回答

您在批注中输入的例外错误信息, 告诉您什么是错的 :

结果错误,未调用结果Set. next

您忘了在访问结果集的第一行前给结果Set. next () 打电话 :

if (rset.next()) {
    System.out.println(rset.getString(1));
}
问题回答

由于您正在使用Netbeans IDE, 您可以尝试连接工具 < a href=" http:// netbeans. org/ kb/docs/ ide/ oracle- db. html" 。 这应该有助于您获得 < code> jdbc_url < /code > 右侧( 这几乎肯定是问题所在 ) 。 另外, 不要忘记下载并在您的类中加入 < a href=" http://www. oracle. com/technetwork/ database/ Incentive- edtion/jdbc-12010-090769. html" rel= “ no folpolation”\"\code> ojdbc6. jar (如果你不包含这个问题, 这肯定是问题 ) 。

e 变量代表例外。 试图打印 e. getMessage () 或用 e. print StackTrace () 打印堆栈跟踪, 以便更好地了解问题 。

 ResultSet rset = stmt.executeQuery();
 while(rset.next()){
     //Code that works with results
 }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签