图书管理系统 用的是MySQL数据库 怎么用Java代码连接数据库

不是驱动器的问题,是代码怎么写,答对给分

驱动
String driver = "com.mysql.jdbc.Driver";

// URL指向要访问的数据库名mydb
String url = "jdbc:mysql://127.0.0.1:3306/mydb";

// 用户名
String user = "root";

// Java连接MySQL密码

String password = "root";

try {
// 加载驱动

Class.forName(driver);

// 连续数据库
Connection conn = DriverManager.getConnection(url, user, password);

if(!conn.isClosed())
System.out.println("connecting to the Database Succee!");

// statement用来执行SQL语句
Statement statement = conn.createStatement();

// 要执行的SQL语句
String sql = "select * from student";

ResultSet rs=statement .executeQuery(sql);
while(rs.hasNext()){
//遍历取得结果
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答