Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

read something from mysql

  Asked By: John    Date: Feb 13    Category: Java    Views: 661
  

Today I encounter a strange problem .
I write a simple program to read data from the mysql database.
Here is the source code:

import java.sql.*;

class Test
{
public static void main(String[] args)
{
String id = "";
String sex = "";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/tmp","root","111");

if (conn!=null)
{
System.out.println("ok,init...");
}
Statement stmt = null;
ResultSet rs = null;
stmt = conn.createStatement();
String sqlstr = "select * from labor;";
rs = stmt.executeQuery(sqlstr);
System.out.println("ok,execute query.");

if (rs==null)
{
System.out.println("no ,it is null.");
}
else{
System.out.println("yes,it has data");
}
while(rs.next()){
id = new String(rs.getString(1));
sex = new String(rs.getString(2));
System.out.println("id="+id+" sex="+sex);
}

}
catch (Exception e)
{
System.out.println("Test Error:"+e);
}
}
}

Compile success.
But when I run it ,it outputs:
ok,init...
ok,execute query.
yes,it has data
id= q sex=
id= r sex=

While actually,my data in the database is:
mysql> select * from labor;
+------+------+
| id | sex |
+------+------+
| id1 | s |
| id2 | i |
+------+------+
2 rows in set (0.01 sec)

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on read something from mysql Or get search suggestion and latest updates.




Tagged: