java连接mysql数据库
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcUtils {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
String url = "jdbc:mysql://localhost:3306/student";
String root = "root";
String password = "123";
return DriverManager.getConnection(url,root,password);
}
public static void closeConnection(Connection con,PreparedStatement ps , ResultSet rs) throws SQLException{
if(con!=null && ps!=null && rs!=null){
con.close();
ps.close();
rs.close();
}
}
}