jdbc连接数据库

String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localost:3306/eshop";
String user = "root";
String password = "123456";
try{
Class.forName(driver);
Connection con = DriverManager.getConnection(url,user,password);
if(!con.isClosed) {
System.out.println("connection succesed");
Statement st = con.createStatement();
String sql ="select * from user";
ResultSet rs = st.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString("Id")+"\t"+rs.getString("name")+"\t"+rs.getString("password"))
}
}
rs.close();
con.close();
} catch() {}

你可能感兴趣的:(jdbc连接数据库)