我正在尝试做这个项目,但它有一个大问题。我创建了带有属性 cid 的表 test2。我通过在 sql 命令行中使用这些命令行将用户添加到 cid 中:
create table test2(cid varchar(20));
insert into test2 values('hello');
select cid from test2;
然后我得到的结果为
CID
-----------------------
HELLO
我的问题是,当我尝试将 jdbc 与 eclipe 一起使用到服务器时,我得到 rs.next() 为 false(rs 是结果集)。显然我在表 test2 中有“hello”。这是我的代码,如果有人可以帮助我解决这个问题,我将不胜感激:
public boolean connect(String username, String password)
{
String connectURL = "jdbc:oracle:thin:@dbhost.xxx.xx.xxx.ca:1522:ug";
try
{
con = DriverManager.getConnection(connectURL,username,password);
System.out.println("\nConnected to Oracle!");
ResultSet rs;
try
{
Statement stmt = con.createStatement();
rs = stmt.executeQuery("SELECT cid FROM test2");
System.out.println("here");
while(rs.next())
{
System.out.println("there");
System.out.println("cid isssssssssss: " + rs.getString("cid"));
}
System.out.println("after here");
stmt.close();
}
catch (SQLException ex)
{
System.out.print("Message: " + ex.getMessage());
}
try {
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return true;
}
catch (SQLException ex)
{
System.out.println("Message: " + ex.getMessage());
return false;
}
}
连接存在,因为当我将 cid 更改为其他内容时,sqlexception 给了我一条错误消息。因此它连接但没有从服务器收到任何查询。
rs = stmt.executeQuery("SELECT something FROM test2");
Connected to Oracle!
Message: ORA-00904: "SOMETHING": invalid identifier
让我困惑。再次感谢。
最佳答案
你问:
I already have the data in the database. I did not insert data from the eclipse but instead I used .sql file to load it to the database. Will that need a commit as well?
我认为您正在使用 SQL*Plus 将数据加载到数据库中。
如果是这样,则需要COMMIT
,除非 SQL*Plus 处于“自动提交”模式。
sql*plus: SQL> show autocommit;
autocommit OFF
SQL> set autocommit ON;
(如果没有“自动提交”或显式的 COMMIT
语句,当您结束 SQL*Plus session 时,事务将回滚。这就是您所观察到的情况的最可能的解释。 )
关于java - JDBC 连接已连接但没有查询结果到结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18198391/