1.导入Jar包
我对Jar包的理解就是各厂商提供的不同驱动混合在一起,用来解决阻抗不匹配问题。讲数据库和程序通过它这个桥连接起来。
a. 准备这些一些东西,Jar包,数据库,开发环境等等。
b. 新建一个项目,建立一个文件夹(JDBC),在所建的项目目录下面新建一个Directory文件夹lib。然后将准备好的Jar包复制粘贴加进去。
c. 在lib包中加入数据包,加入完之后就会如图所示。
d. 然后就准备完成了,下面样例如图
2.加载驱动
加载驱动,固定写法没有什么特别的。
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
3. 用户信息
url地址: url地址基本上也是固定写法,jdbc:mysql:localhost:3306是固定的写法 + 数据库名称 + ? +使用Unicode编码防止乱码 + 设置字符集 + 使用安全连接。如果连接失败把安全连接的true改为false;问号连接参数的,我用的是Mysql数据库,jdbc应该是协议就像http一样,前面的mysql应该是一个数据库的提供商,localhost是本地,3306是端口。
username: 登入数据库的用户名。
password:登入数据库的用户名密码
例如连接school数据库写法如下。
String url = "jdbc:mysql:localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true"
String username = "root"; //登入数据库的用户名
String password = "123456"; //登入数据库的用户名密码
4. 连接数据库
固定写法,连接完就可以认为con代表刚才连接的数据库。
Connection con = null;
try {
con = DriverManager.getConnection(url,username,password);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
5. 创建执行SQL语句的对象
利用刚才连接的数据库con变量创建执行SQL语句的对象分为三种:
-
Statement:Statement 用于通用查询。
-
PreparedStatement: PreparedStatement用于执行参数化查询,
-
CallableStatement:CallableStatement则是用于存储过程。
现在PreparedStatement使用较多,对比参考连接
对SQL语句的操作最后只留下两种:
a. 第一种是查询的executeQuery语句
b. 另一个是包括更新,插入,删除的executeUpdate语句。
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(); //返回查询的结果集
int rs = st.executeUpdate(); //更新失败
6. 执行SQL语句
查询结果集rs可以利用字段匹配将数据信息输出出来, rs.next()表示有下一条数据那么就一直循环输出结果集。当输出时如果不知道具体是什么数据类型可以写getObject(“字段”);这样就会自动匹配数据类型;还是挺人性化的。
String query = "select *from student";
ResultSet rs = st.executeQuery(query);
while(rs.next()){ //存在下一条数据
System.out.print("id = "+rs.getObject("id"));
System.out.print(", name = "+rs.getObject("name"));
System.out.print(", age = "+rs.getObject("age"));
System.out.printf("\n==========================================\n");
}
和上面的查询数据结果一样。
7. 释放连接
从尾部释放,先释放结果集rs,再释放执行SQL语句的对象st,最后关闭数据库的连接con,不然会导致内存占用严重。
package study.jdbc;
import java.sql.*;
public class JDBCfirst {
public static void main(String[] args) throws SQLException {
// 1. 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver"); //固定写法
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 2. url username password
String url = "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "root";
String password = "123456";
// 3. 连接成功 数据库对象 Connection代表数据库
Connection con = null;
try {
con = DriverManager.getConnection(url,username,password);
//4. 执行sql对象 Statement 执行sql的对象
Statement st = con.createStatement();
//5. 用sql的对象 去执行sql语句,可能存在结果,查看返回结果
String query = "select *from student";
ResultSet rs = st.executeQuery(query);
while(rs.next()){
System.out.print("id = "+rs.getObject("id"));
System.out.print(", name = "+rs.getObject("name"));
System.out.print(", age = "+rs.getObject("age"));
System.out.printf("\n==========================================\n");
}
rs.close(); //释放连接
st.close(); //释放连接
con.close(); //释放连接
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
愿你走出半生,归来仍是少年~