赞
踩
目录
Java数据库连接,(Java Database Connectivity,简称JDBC)是java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法,我们通常说的JDBC是面向关系型数据库的。
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.29</version>
- </dependency>
注意:在使用JDBC之前要保证数据库远程连接服务启动!
1.Class.forName()加载驱动
2.DriverManager获取Connection连接
3.创建Statement执行SQL语句
4.增删改操作使用preparedStatement.executeUpdate();返回影响的行数
查询操作使用preparedStatement.executeQuery();返回查询结果
5.释放资源
首先创建表:
create table if not exists dog ( id int(10) primary key auto_increment, name varchar(32) comment '宠物名', health int(10) comment '健康值', love int(10) comment '亲密度', strain varchar(32) comment '品种', lytime datetime comment '领养时间' );
- package jdbcstu;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
-
- public class Dog {
- public static void main(String[] args) {
- Connection connection = null;
- PreparedStatement preparedStatement = null;
- try {
- // com.mysql.jdbc.Driver
- // 第一步:加载驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 第二步:获取Connection对象
- connection = DriverManager.getConnection(
- "jdbc:mysql://192.168.180.141:3306/jdbcstudb", // mysql服务器地址,端口,数据库名
- "root", // 数据库的用户名
- "root" // 数据库密码
- );
- System.out.println(connection);
- // 第三步:操作数据库 增删改查
- /*新增*/
- // String sql = "insert into dog(name,health,love,strain,lytime) values(?,?,?,?,?)";
- String sql = "insert into dog(name,health,love,strain,lytime) values(?,?,?,?,now())";
- String name = "ggg";
- Integer health = 92;
- Integer love = 66;
- String strain = "斗牛";
- // Date date = new Date(System.currentTimeMillis());// 只能存年月日
-
- // 第三步:创建preparedstatement
- preparedStatement = connection.prepareStatement(sql);
- preparedStatement.setString(1,name);
- preparedStatement.setInt(2,health);
- preparedStatement.setInt(3,love);
- preparedStatement.setString(4,strain);
- // preparedStatement.setDate(5,date);
-
- // 第四步:获取到执行新增语句后的返回结果
- int num = preparedStatement.executeUpdate(); // 返回影响的行数
- if(num > 0){
- System.out.println("新增宠物狗成功!");
- }
- }catch (ClassNotFoundException e){
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- // 第五步:释放资源
- try {
- if (preparedStatement != null) {
- preparedStatement.close();
- }
- if (connection != null) {
- connection.close();
- }
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- package jdbcstu;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
-
- public class Dog {
- public static void main(String[] args) {
- Connection connection = null;
- PreparedStatement preparedStatement = null;
- try {
- // com.mysql.jdbc.Driver
- // 第一步:加载驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 第二步:获取Connection对象
- connection = DriverManager.getConnection(
- "jdbc:mysql://192.168.180.141:3306/jdbcstudb", // mysql服务器地址,端口,数据库名
- "root", // 数据库的用户名
- "root" // 数据库密码
- );
- System.out.println(connection);
- // 第三步:操作数据库 增删改查
- String sql = "delete from dog where name = ? and health = ?";
- preparedStatement = connection.prepareStatement(sql);
- String name = "ggg";
- Integer health = 92;
- preparedStatement.setString(1,name);
- preparedStatement.setInt(2,health);
- int num = preparedStatement.executeUpdate();
- if(num>0){
- System.out.println("删除成功!"+num);
- }
- }catch (ClassNotFoundException e){
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- // 第五步:释放资源
- try {
- if (preparedStatement != null) {
- preparedStatement.close();
- }
- if (connection != null) {
- connection.close();
- }
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- package jdbcstu;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
-
- public class Dog {
- public static void main(String[] args) {
- Connection connection = null;
- PreparedStatement preparedStatement = null;
- try {
- // com.mysql.jdbc.Driver
- // 第一步:加载驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 第二步:获取Connection对象
- connection = DriverManager.getConnection(
- "jdbc:mysql://192.168.180.141:3306/jdbcstudb", // mysql服务器地址,端口,数据库名
- "root", // 数据库的用户名
- "root" // 数据库密码
- );
- System.out.println(connection);
- // 第三步:操作数据库 增删改查
- String sql = "update dog set name = ?,health = ?,love = ? where id = ?";
- preparedStatement = connection.prepareStatement(sql);
- preparedStatement.setString(1,"二哈");
- preparedStatement.setInt(2,99);
- preparedStatement.setInt(3,80);
- preparedStatement.setInt(4,2);
- int num = preparedStatement.executeUpdate();
- if(num > 0){
- System.out.println("修改宠物狗成功!");
- }
- }catch (ClassNotFoundException e){
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- // 第五步:释放资源
- try {
- if (preparedStatement != null) {
- preparedStatement.close();
- }
- if (connection != null) {
- connection.close();
- }
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- package jdbcstu;
-
- import java.sql.*;
-
- public class Dog {
- public static void main(String[] args) {
- Connection connection = null;
- PreparedStatement preparedStatement = null;
- ResultSet resultSet = null;
- try {
- // com.mysql.jdbc.Driver
- // 第一步:加载驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 第二步:获取Connection对象
- connection = DriverManager.getConnection(
- "jdbc:mysql://192.168.180.141:3306/jdbcstudb", // mysql服务器地址,端口,数据库名
- "root", // 数据库的用户名
- "root" // 数据库密码
- );
- System.out.println(connection);
- // 第三步:操作数据库 增删改查
- String sql = "select id,name,health,love,strain,lytime from dog where health = ? and strain = ? limit ?,?";
- preparedStatement = connection.prepareStatement(sql);
- preparedStatement.setInt(1,99);
- preparedStatement.setString(2,"哈士奇");
- preparedStatement.setInt(3,0);
- preparedStatement.setInt(4,2);
- resultSet = preparedStatement.executeQuery();
- System.out.println("\t\t\t狗狗信息");
- System.out.println("编号\t宠物名\t健康值\t亲密度\t品种\t\t领养时间");
- while (resultSet.next()){
- System.out.print(resultSet.getInt("id")+"\t");
- System.out.print(resultSet.getString("name")+"\t\t");
- System.out.print(resultSet.getInt("health")+"\t\t");
- System.out.print(resultSet.getInt("love")+"\t\t");
- System.out.print(resultSet.getString("strain") + "\t\t");
- System.out.println(resultSet.getDate("lytime")+"\t");
- }
- }catch (ClassNotFoundException e){
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- // 第五步:释放资源
- try {
- if (preparedStatement != null) {
- preparedStatement.close();
- }
- if (connection != null) {
- connection.close();
- }
- if(resultSet != null){
- resultSet.close();
- }
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- package jdbcstu;
-
- import java.sql.*;
-
- public class Master {
- public static void main(String[] args) {
- Connection connection = null;
- PreparedStatement preparedStatement = null;
- ResultSet resultSet = null;
- try {
- // 第一步:加载驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 第二步:获取connection对象
- connection = DriverManager.getConnection(
- "jdbc:mysql://192.168.180.141:3306/jdbcstudb",
- "root",
- "root"
- );
- System.out.println(connection);
- // 第三步:操作数据库 增删改查
- /*查询数据*/
- String sql = "select m.name ,\n" +
- " m.age ,\n" +
- " m.gender ,\n" +
- " m.yearnum ,\n" +
- " d.name ,\n" +
- " d.health ,\n" +
- " d.love \n" +
- " from master m\n" +
- "join dog d on m.did = d.id\n" +
- "where m.age > ?\n" +
- "order by d.id desc\n" +
- "limit ?";
- preparedStatement = connection.prepareStatement(sql);
- preparedStatement.setInt(1,30);
- preparedStatement.setInt(2,2);
- resultSet = preparedStatement.executeQuery();
- System.out.println("\t\t\t领养信息");
- System.out.println("主人姓名\t主人年龄\t主人性别\t主人经验值\t宠物名\t宠物健康值\t宠物亲密度");
- while (resultSet.next()){
- System.out.print(resultSet.getString("m.name")+"\t\t");
- System.out.print(resultSet.getInt("m.age")+"\t\t");
- System.out.print(resultSet.getString("m.gender")+"\t\t");
- System.out.print(resultSet.getInt("m.yearnum") + "\t\t\t");
- System.out.print(resultSet.getString("d.name")+"\t\t");
- System.out.print(resultSet.getInt("d.health")+"\t\t\t");
- System.out.println(resultSet.getInt("d.love")+"\t\t");
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- try {
- if (connection != null) {
- connection.close();
- }
- if(preparedStatement != null){
- preparedStatement.close();
- }
- if(resultSet != null){
- resultSet.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。