【问题标题】:How to retrieve data from MySQL database using jdbc?如何使用 jdbc 从 MySQL 数据库中检索数据?
【发布时间】:2012-03-22 19:11:05
【问题描述】:

我正在尝试使用 eclipse 从 MySQL 数据库中检索数据。我正在使用与 java 应用程序相同的 JDBC 代码...但它确实有效。

【问题讨论】:

  • 如果您在这里发布一些代码会有所帮助..
  • 我相信您正在为 mysql 更改 Class.forName...检查您得到的异常...

标签: java mysql eclipse jdbc


【解决方案1】:

你可以这样做:

  Class.forName("com.mysql.jdbc.Driver"); // Setup the connection with the DB
  connect = DriverManager.getConnection("jdbc:mysql://localhost/database_name", username,password);  

// Statements allow to issue SQL queries to the database ; that's why we need to create one.
    statement = connect.createStatement();

// Result set get the result of the SQL query
resultSet = statement.executeQuery("select * from TableName;");

    while (resultSet.next()) {  //retrieve data
        String data = resultSet.getString("column_name");
                   ...
     }

【讨论】:

    【解决方案2】:

    请确保您的数据库连接已成功创建,然后应用查询从数据库中检索数据。如果您的连接没有创建,那么您需要检查您的驱动程序设置或 mysql 密码设置。

    【讨论】:

      【解决方案3】:

      Class.forName("com.mysql.jdbc.Driver"); 连接 con=DriverManager.getConnection("jdbc:mysql://localhost/database_name",username,password");

      具有类似'createstatement()'的方法的连接类,借助它我们可以从数据库中查询。

      语句语句 = con.createstatement(); resultSet=statement.executeQuery("select * from tablename");

      最后从“resultSet”对象中检索数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-04
        • 2015-01-20
        • 1970-01-01
        • 2015-12-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多