java数据库编程中数据的查询:
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.omg.PortableServer.ID_ASSIGNMENT_POLICY_ID;
public class ResultSetDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
java.sql.Statement stmt = null;
ResultSet rs = null;
String sql="select * from student";
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
// TODO: handle exception
}
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost/student?user=root&password=123456");
System.out.println("数据库连接成功");
stmt=conn.createStatement();
rs=((java.sql.Statement) stmt).executeQuery(sql);
System.out.println("获得查询");
int i=1;
while(rs.next())
{
System.out.println("第"+(i++)+"条记录");
System.out.println("id="+rs.getString(1)+"name="+rs.getString(2));
}
} catch (SQLException e1) {
e1.printStackTrace();
// TODO: handle exception
}
finally
{
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (Exception e) {
// TODO: handle exception
}
}
System.out.println("over");
}
}