Java连接MySQL的问题

发布于 2024-10-10 07:56:01 字数 1069 浏览 0 评论 0原文

我尝试从数据库中获取一些数据。连接方法肯定有效,但我在从数据库获取任何数据时遇到问题:

    SQLConnect s = new SQLConnect();
    Connection c = s.getConnection();

    Statement st = c.createStatement();

    ResultSet rs = st.executeQuery("select * from produkty");
    System.out.println(rs.getString(2));

问题出在最后一行(当我评论它时,没有出现错误)。 错误消息:

Connected to database
Exception in thread "main" java.sql.SQLException: Before start of result set
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5656)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5576)
    at antmedic.Main.main(Main.java:85)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

感谢您的帮助

I try to get some data form database. The connection method works for sure, but I have a problem getting any data form DB:

    SQLConnect s = new SQLConnect();
    Connection c = s.getConnection();

    Statement st = c.createStatement();

    ResultSet rs = st.executeQuery("select * from produkty");
    System.out.println(rs.getString(2));

The problem is with the last line (when I comment it, no error appears).
Error message:

Connected to database
Exception in thread "main" java.sql.SQLException: Before start of result set
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5656)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5576)
    at antmedic.Main.main(Main.java:85)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉南桥 2024-10-17 07:56:01

您需要调用 ResultSet#next() 将结果集光标移动到下一行。通常,当存在多行时,请在 while 循环中执行此操作。

while (rs.next()) {
    System.out.println(rs.getString(2));
}

或者,当您期望零行或一行时,请使用 if 语句。

if (rs.next()) {
    System.out.println(rs.getString(2));
}

另请参阅:

You need to call ResultSet#next() to shift the resultset cursor to the next row. Usually, when there's means of multiple rows, do this in a while loop.

while (rs.next()) {
    System.out.println(rs.getString(2));
}

Or when you expect zero or one row, use an if statement.

if (rs.next()) {
    System.out.println(rs.getString(2));
}

See also:

梦里泪两行 2024-10-17 07:56:01

当您获取 ResultSet 对象时,光标指向第一行之前的行,因此调用后

while (rs.next()) {
//your code
}

光标指向下一行

即第一行。

As you get the ResultSet object, the cursor points to the row before the first row, So after calling

while (rs.next()) {
//your code
}

the cursor points to the next row

i.e. the first row.

苍暮颜 2024-10-17 07:56:01

请记住,每当触发 select 查询以将数据从数据库检索到 ResultSet 时,
所以ResultSet的结构是

->零记录区

->数据库记录区

->没有记录区域

,这就是为什么我们必须将 next() 与 ResultSet 对象放在一起,以便它可以从零记录区域移动到数据库记录区域。

Remember, whenever select query fires for retrieving the data from database into ResultSet,
so the structure of ResultSet is

-> Zero Record Area

-> Database Record Area

-> No Record Area

that's why alwayz we must put next() with ResultSet object so it can move from Zero Record Area to Database Record Area.

暮色兮凉城 2024-10-17 07:56:01
     while(rs.next())
     {
        System.out.println(rs.getString(1));
        System.out.println(rs.getString(2));
     }

在 1, 2, .... 的地方,我们也可以使用数据库列名称。但从技术上讲,我们总是使用像 1,2,3,.... 这样的索引,这是我们数据库将来发生任何更新的原因,例如更改列名称,因此对我们来说不会出现任何问题,因为我们没有使用列名称。

     while(rs.next())
     {
        System.out.println(rs.getString(1));
        System.out.println(rs.getString(2));
     }

at the place of 1, 2, .... we can use the database columns name also. But technically always we use indexing like 1,2,3,.... its reason for any updation happening in future at our database like changes the column name so it can't be occur any problem for us because we haven't used the columns names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文