JAVA数据库使用(Mysql)
package Sql;
import java.sql.*;
public class mainSql {
public static void main(String[] args) {
Connection con;
String driver="com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/Liaogui";
String user ="root";
String password="liaogui";
try{
Class.forName(driver);
con=DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the database");
Statement statement = con.createStatement();
String sql ="select * from T_user";
ResultSet rs = statement.executeQuery(sql);
System.out.println("----------");
System.out.println("程序结果运行如下:");
System.out.println("姓名"+"\t"+"职称");
String job=null;
String id =null;
while(rs.next()){
job =rs.getString("Job");
id =rs.getString("name");
System.out.println(id+"\t"+job);
}
rs.close();
con.close();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("数据库数据获取成功");
}
}
}