import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class QueryDemo {
public static void main(String[] args){
String JDriver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://127.0.0.1:3306/test";
String user="root";
String password="root";
try{
Class.forName(JDriver);// 动态导入数据库的驱动
Connection conn=DriverManager.getConnection(url, user, password);// 获取数据库链接
String query="SELECT * FROM student";// 创造SQL语句
Statement stmt=conn.createStatement();// 执行SQL语句
ResultSet rs=stmt.executeQuery(query);
while(rs.next()){
System.out.println(rs.getString("no")+":"+rs.getString(2));
//密码字段的编号从1开始,密码排第二位
}
System.out.println("查询数据成功");
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
} |
|