图书管理系统 用的是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()){
//遍历取得结果
}