第一个JDBC程序

mysql连接器下载

下载连接器

下载地址:https://downloads.mysql.com/archives/installer/

将连接器放入idea

打开idea新建项目,在项目里创建一个lib的文件
把下载好的连接器复制到lib文件下
然后点击lib文件右击选择And to Library 添加到库里,这是lib文件下的连接器文件应该是能展开的。

DBC连接数据库

基本步骤

第一步:加载驱动
第二步:获取数据库的信息和url
第三步:连接数据库返回一个数据库对象
第四步:执行sql对、
第四步:执行sql命令,获得查询的返回一个结果集
第五步: 通过返回结果集输出查询结果
第六步:释放连接,先开启后释放的顺序

JDBC常用API(应用程序编程接口)

Connection 接口 Statement 接口 ResulSet 接口 Driver 接口 PreparedStatement 接口

Connection 接口

Statement 接口

ResulSet 接口

Driver 接口

PreparedStatement 接口

第一个JDBC 程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.guolu;
import java.sql.*;
public class lesson001 {
public static void main(String[] args) throws SQLException,ClassNotFoundException{
//1.加载数据驱动;
Class.forName("com.mysql.jdbc.Driver");
//2.获得用户信息(用户名,用户密码),url;
String username="root";
String password="123456";
String url="jdbc:mysql://localhost:3306/school";
//3.Connection 获得数据库的对象;
Connection connection= DriverManager.getConnection(url,username,password);
//4.Statement 执行数据库;
Statement statement=connection.createStatement();
// 5.ResultSet 返回一个结果集;
String sql= "SELECT * FROM category";
ResultSet resultset= statement.executeQuery(sql);
//6.通过结果集查询数据库的信息;
System.out.printf("查询成功!");
System.out.printf("查询成功!");
while (resultset.next()) {
System.out.print("科目代号:" + resultset.getObject("categoryid"));
System.out.print("\t");
System.out.print("科目名:" + resultset.getObject("categoryName"));
System.out.printf("");
}
//7.关闭连接,释放资源;
resultset.close();
statement.close();
connection.close();
}
}

提取工具类

笔记来自->学习视频:遇见狂神说

创建一个配置文件

  1. 配置文件名的扩展名为.properties
    配置文件最好创建在src文件下的可以通过反射难道,再调用时会比较方便,如果放在当前目录的子目录下就需要一层层的查找
  2. 在配置文件里添加驱动、url、用户名、用户密码。
    1
    2
    3
    4
    5
    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSl=true
    username=root
    password=123456

编写工具类

在src文件下创建一个Java类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.lesson02.utils;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
private static String driver = null;
private static String username=null;
private static String password=null;
private static String url=null;
static {
try {
//class.getClassLoader()获得类加载器,getResourceAsStream("db.properties")获得配置文件的资源;
//返回的是一个输入流,用InputStream存储;
InputStream in=JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties=new Properties();
properties.load(in);
driver=properties.getProperty("driver");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
//加载驱动;
Class.forName("driver");
}catch (Exception e){
e.printStackTrace();
}
}
//返回数据库对象
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
//释放资源;
public static void release(Connection con, Statement st, ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (st!=null){
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con!=null){
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}

编写测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.lesson02;
import com.lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TextInset {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
//获取数据库对象;
conn=JdbcUtils.getConnection();
//获取数据库操作对象;
st=conn.createStatement();
String sql="INSERT INTO SUBJECT(subjectno,`subjectname`,`classhour`,`gradeid`) VALUES(18,'网络测试','100','2')";
//执行sql;
int i=st.executeUpdate(sql);
if(i>0){
System.out.printf("插入成功!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//释放资源;
JdbcUtils.release(conn,st,rs);
}
}
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 jaytp@qq.com

×

喜欢就点赞,疼爱就打赏