JDBC连接数据库——对数据库表增删改查

  • Post author:
  • Post category:其他




什么是JDBC

java database connectivity Java数据库连接



在dependencies标签中配置数据库驱动信息

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<!--JDBC驱动-->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.40</version>
</dependency>



JDBC连接数据库步骤


1.加载驱动

Class.forName(com.mysql.jdbc.Driver);

Class.forName 方法的作用,就是初始化给定的类。

Class.forName的作用

  • 1.装载一个类并且对其进行实例化的操作。
  • 2.装载过程中使用到的类加载器是当前类。


2.建立连接

 Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/student""root""root")

jdbc:mysql://localhost:3306/student

使用的技术:数据库名称://主机地址:数据库端口号/要使用的数据库名称

DriverManager 管理JDBC(java database connectivity)驱动

Connection 连接 Connection对象用于创建一个到达某个数据源的开放连接。通过此连接,您可以对一个数据库进行访问和操作。



外部文件加载驱动连接驱动步骤


1.创建resources文件夹


右击resources文件夹选择 Mark Directory as 再点击Test Resources Root


2.创建后缀名为.properties的文件


3.在文件中写配置信息


1.加载驱动配置信息

DriverClass=com.mysql.jdbc.Driver

2.连接数据配置信息

url=jdbc:mysql://localhost:3306/student
user=root
password=root



使用自建的JDBC工具类连接数据库


1.创建一个JDBCUtil类


2.创建一个static修饰带有Connection返回值方法,并在方法中创建Properties对象

//1.创建Properties对象
 Properties properties = new Properties();


3.读取外部后缀名为.properties的文件、获取文件内容


读取文件

//读取外部后缀名为.properties的文件
properties.load(JDBCutil.class.getClassLoader().getResourceAsStream("JDBC.properties"));//这里的JDBC.properties是到读取的外部文件名称

获取文件内容

  //获取文件内容
String driverClass = properties.getProperty("DriverClass");//这里的driverClass是.properties文件中的属性名
String url = properties.getProperty("url");//这里的url是.properties文件中的属性名
String user = properties.getProperty("user");//这里的user是.properties文件中的属性名
String password = properties.getProperty("password");//这里的password是.properties文件中的属性名


4.加载驱动

Class.forName(driverClass);//这里的driverClass是.properties文件中的属性名


5.连接数据库

Connection connection = DriverManager.getConnection(url, user, password);

DriverManager 管理JDBC(java database connectivity)驱动

Connection 连接 Connection对象用于创建一个到达某个数据源的开放连接。通过此连接,您可以对一个数据库进行访问和操作。


6.创建关闭资源方法


创建一个static修饰方法 两个形式参数PreparedStatement,Connection在方法中关闭资源



使用JDBC对数据库表增删改


1.连接数据库

 Connection connection = JDBCutil.getConnection();

connection 负责连接并传输数据

调用JDBCutil(JDBC工具类)中的方法,这个方法返回一个connection


2.编写带有?占位符SQL语句


3.预编译SQL

PreparedStatement preparedStatement = connection.prepareStatement(sql);

preparedStatement 由connection产生

preparedStatement 执行SQL语句


4.给占位符填充值

preparedStatement.setObject();


5.执行操作(增删改)

preparedStatement.executeUpdate();


6.关闭资源

JDBCutil.resourcesClose(preparedStatement, connection);



使用JDBC对数据库表查询


1.连接数据库

 Connection connection = JDBCutil.getConnection();

connection 负责连接并传输数据

调用JDBCutil(JDBC工具类)中的方法,这个方法返回一个connection


2.编写带SQL语句


3.预编译SQL

PreparedStatement preparedStatement = connection.prepareStatement(sql);

preparedStatement 由connection产生

preparedStatement 执行SQL语句


4.执行操作,获取结果集

ResultSet resultSet = preparedStatement.executeQuery();


5.处理结果集 遍历结果集

while (resultSet.next()) {
    int id = resultSet.getInt("id");
    System.out.println(id);
}


6.输入结果集


7.关闭资源

resultSet.close();
JDBCutil.resourcesClose(preparedStatement, connection);



版权声明:本文为CSMCZM原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。