1:导入 mysql-connector-java-5.0.8-bin.jar(可以是不一样的版本)
2:写代码,新建一个类,创建main方法进行连接Mysql数据库.
A代码块
import java.sql.*;
public class JdbcTest01
{
public static void main(String[] args) throws SQLException {
//1、注册数据库的驱动
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//2、获取与数据库的连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "jhs123");
System.out.println("看看连上了吗??"+conn.getClass());//3、创建代表SQL语句的对象 Statement stmt = conn.createStatement(); //4、执行SQL语句 ResultSet rs = stmt.executeQuery("select id,name,password,email,birthday from users"); //5、如果执行的是查询语句,要对结果进行处理 while(rs.next()){ System.out.println("--------------------"); System.out.println(rs.getObject("id")); System.out.println(rs.getObject("name")); System.out.println(rs.getObject("password")); System.out.println(rs.getObject("email")); System.out.println(rs.getObject("birthday")); } //6、释放占用的资源 rs.close();//以打开的顺序的相反顺序进行关闭 stmt.close(); conn.close(); } }
如果你只是想看一下连上数据库了吗?--------------------------你可以这么做.
上述代码中的:System.out.println("看看连上了吗??"+conn.getClass());
一下部分可以全部要,运行,看控制台是否输出:
![]()
有,恭喜你 成功连上数据库!!!!!
下面是对上述代码的说明,如果你只是想要功能,恭喜你 这里已经结束了!
- DriverManager:
作用1:注册驱动;获取与数据库的连接。
注册驱动:
方式一:DriverManager.registerDriver(new com.mysql.jdbc.Driver());
缺点:严重依赖驱动;导致驱动注册多次;
方式二(建议):Class.forName(“com.mysql.jdbc.Driver”);
将A代码块,中的取动方式替换成方式二的代码....
作用2:获取与数据库的连接 3种方式:
方式一:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day15", "root", "sorry");
"jdbc:mysql://localhost:3306/day15":数据库连接的URL。不同数据库的不同版本连接串也是不同的(具体参考数据库的说明文档)。
jdbc:mysql://localhost:3306/day15=jdbc:mysql:///day15(默认连接本机的3306端口)
方式二:

方式三:
![]()

JDBC 连接模板。。。
//JDBC Connect Template
@Test
public void template() {
Connection conn = null;
Statement stmt = null;
int num = 0;// 更新 操作
ResultSet rs = null;
try {
//1、注册数据库的驱动
//Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//2、获取与数据库的连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "jhs123");
System.out.println("看看连上了吗??" + conn.getClass());
//3、创建代表SQL语句的对象
stmt = conn.createStatement();
//4、执行SQL语句
num = stmt.executeUpdate("UPDATE users SET password='123'");
System.out.println(num);
rs = stmt.executeQuery("select id,name,password,email,birthday from users");
//5、如果执行的是查询语句,要对结果进行处理
while (rs.next()) {
// void getObject(int fieldIndex):按照字段的索引取数据。第一个字段就是1
//void getObject(String fieldName):按照字段名来取数据。
System.out.println("--------------------");
System.out.println(rs.getObject("id"));
System.out.println(rs.getObject("name"));
System.out.println(rs.getObject("password"));
System.out.println(rs.getObject("email"));
System.out.println(rs.getObject("birthday"));
// rs.getObject(1);//第一个字段从1开始
// rs.getObject(2);
// rs.getObject(3);
// rs.getObject(4);
// rs.getObject(5);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//6、释放占用的资源
//以打开的顺序的相反顺序进行关闭
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
本文详细介绍了如何使用Java通过mysql-connector-java驱动连接MySQL数据库,包括导入jar包、编写代码实现连接、执行SQL语句及处理结果集的过程。
1899

被折叠的 条评论
为什么被折叠?



