2012-03-05 110 views
-2

我想使用eclipse从MySQL数据库检索数据。我使用JDBC的相同代码作为Java应用程序......但它确实有效。如何使用jdbc从MySQL数据库检索数据?

+4

会。如果你在这里发布了一些代码,请帮忙.. – 2012-03-05 09:49:28

+0

我相信你一个重新更改MySQL的'Class.forName' ...检查你得到的异常... – 2012-03-05 10:26:03

回答

0

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

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"); 
        ... 
    } 
0

的Class.forName( “com.mysql.jdbc.Driver”);连接con = DriverManager.getConnection(“jdbc:mysql:// localhost/database_name”,用户名,密码“);

有类似'createstatement()'方法的连接类,在这个帮助下我们可以从数据库中查询。

声明语句= con.createstatement(); 的resultSet = Statement.executeQuery的( “从表名SELECT *”);

终于从 '结果集' 对象retrive数据