java是怎么连接mysql数据库的
1个回答
展开全部
import java.sql.*;
public class Dao {
protected static String dbClassName = "com.mysql.jdbc.Driver";
protected static String dbUrl = "jdbc:mysql://localhost/libraryInformation";
protected static String dbUser = "root";
protected static String dbPwd = "mysql";
protected static String second = null;
private static Connection conn = null;
//创建数据库连接
private Dao() {
try {
if (conn == null) {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
System.out.println("数据库连接成功!");
}
else
return;
} catch (Exception ee) {
ee.printStackTrace();
}
}
//关闭数据库
public static void close() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
}
记得把MySQL的驱动包导入项目里就OK了
满意请采纳!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询