Java 如何使用jdbc从MySQL数据库检索数据?

Java 如何使用jdbc从MySQL数据库检索数据?,java,mysql,eclipse,jdbc,Java,Mysql,Eclipse,Jdbc,我正在尝试使用eclipse从MySQL数据库检索数据。我使用的JDBC代码与java应用程序的代码相同……但它确实有效。请确保成功创建了数据库连接,然后应用查询从数据库检索数据。如果未创建连接,则需要检查驱动程序设置或mysql密码设置。您可以执行以下操作: Class.forName("com.mysql.jdbc.Driver"); // Setup the connection with the DB connect = DriverManager.getConnection("

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

请确保成功创建了数据库连接,然后应用查询从数据库检索数据。如果未创建连接,则需要检查驱动程序设置或mysql密码设置。

您可以执行以下操作:

  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");
                   ...
     }
Class.forName(“com.mysql.jdbc.Driver”); Connection con=DriverManager.getConnection(“jdbc:mysql://localhost/database_name“,用户名,密码”)

连接类具有类似“createstatement()”的方法,借助于此,我们可以从数据库中查询

语句Statement=con.createstatement(); resultSet=statement.executeQuery(“从表名中选择*)


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

如果您在此处发布一些代码,这会有所帮助。.我相信您正在更改
类。对于mysql,forName
…检查您得到的异常。。。