Java 中的 MySQL 连接问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4588334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:05:38  来源:igfitidea点击:

MySQL connection in Java problem

javamysqljdbc

提问by Devel

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

我尝试从数据库中获取一些数据。连接方法肯定有效,但我在获取任何数据表单 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

谢谢你的帮助

回答by BalusC

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 whileloop.

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

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

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

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

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

See also:

也可以看看:

回答by Ankit

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

当你拿到ResultSet对象时,光标指向第一行之前的行,所以调用之后

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

the cursor points to the next row

光标指向下一行

i.e. the first row.

即第一行。

回答by Rohit Pauranik

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

请记住,无论何时触发选择查询以将数据从数据库检索到 ResultSet,因此 ResultSet 的结构是

-> 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 Areato Database Record Area.

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

回答by Rohit Pauranik

     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.

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

回答by manikant gautam

put down your code like that

像这样放下你的代码

while(rs.next())
{
  String abc=rs.getString("abc");
}