使用纯java方式连接mysql数据库

import java.sql.Connection;
import java.sql.DriverManager;    //导包自行修改
import java.sql.SQLException;
import java.sql.Statement;


public class Test {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
// 1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 2.建立连接
try {
conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/schooldb", "root", "sasa");
System.out.println("数据库连接成功");
conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 3.关闭连接
try {
if (conn != null) {
conn.close();
}
if (stmt != null) {
stmt.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}


}

猜你喜欢

转载自blog.csdn.net/lly987505445/article/details/79770105