1. 通过Database进行连接
1.1. 选择数据库种类
1.2. 填写数据库信息
1.3. 测试与应用
2. Java程序连接数据库
读取数据库内容 Java程序
import java.sql.*;
public class nannan {
public static void main(String[] args) {
Connection con ;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/ruoyi?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useAffectedRows=true&allowMultiQueries=true";
String user = "root";
String password = "root";
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("数据连接成功!");
Statement dosql = con.createStatement();
String sql = "select * from sys_user_role";
ResultSet rs = dosql.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-------------------------------------------");
System.out.println("用户ID"+"\t角色ID");
System.out.println("-------------------------------------------");
String user_id = null;
String role_id = null;
while(rs.next()){
user_id=rs.getString("user_id");
role_id = rs.getString("role_id");
System.out.println(user_id+"\t"+role_id);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
System.out.println("找不到驱动程序!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
finally{
}
}
}
运行结果
|