怎么用jdbc和mysql数据库连接

如题所述

String driverName = "com.mysql.jdbc.Driver"; // 加载JDBC驱动
String dbURL = "jdbc:mysql://localhost:3306/test"; // 连接服务器和数据库test
String userName = "sa"; // 默认用户名
String userPwd = "sa"; // 密码
java.sql.Connection dbConn;
 
try {
    Class.forName(driverName).newInstance();
    dbConn = java.sql.DriverManager.getConnection(dbURL, userName, userPwd);
    System.out.println("Connection Successful!");  //如果连接成功 控制台输出Connection Successful!
} catch (ClassNotFoundException e) {
    System.out.println("没有找到驱动");
} catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
}


还是直接上代码吧。

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答