JDBC的基本使用(mysql与java)

目录

1、JDBC简介

使用JDBC操作数据库的基本步骤

2、JDBC常用的类

(1)创建接口对象

 (2)ResultSet接口常用方法

3、连接数据库

 (1)加载驱动程序

 (2)连接数据库

(3)连接数据库常见错误

4、动态数据查询

5、 增修删数据库


1、JDBC简介

 一种Java与数据库连接的技术

Java和数据库并不是直接连接,而是通过mysql提供的jar包进行连接

使用JDBC操作数据库的基本步骤

关于jar包的下载以及配置详情配置见 :JDBC在idea上的配置

2、JDBC常用的类

(1)创建接口对象

什么是结果集?

可以理解为数据库和程序之间交流的通道

 (2)ResultSet接口常用方法

列名是从第一列开始 

package MYSQLStudy;

import java.sql.*;

public class Demo1 {
    static Statement statement;
    static ResultSet resultSet;
    static Connection connection;
    public static void main(String[] args) throws SQLException {

        try {
            Class.forName(("com.mysql.cj.jdbc.Driver"));
            String url="jdbc:mysql://127.0.0.1:3306/text1?serverTimezone=GMT";
            String username = "root";
            String password = "QAZWSX564368";
             connection=DriverManager.getConnection(url,username,password);
            if(connection!=null){
                System.out.print("连接成功");
            }
             statement=connection.createStatement();//创建statement对象
             resultSet= statement.executeQuery("select* from student");
            while (resultSet.next()){
                int id=resultSet.getInt("sno");
                String name=resultSet.getString(2);
                Date bdate=resultSet.getDate("bdate");
                System.out.println(id);
                System.out.println(name);
                System.out.println(bdate);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            if (resultSet!=null) resultSet.close();
            if (statement!=null) statement.close();
            if (connection!=null) connection.close();
        }


    }
}

 

3、连接数据库

 (1)加载驱动程序

 Class.forName(("com.mysql.cj.jdbc.Driver"));//有些教程里面没有cj

 (2)连接数据库

 String url="jdbc:mysql://127.0.0.1:3306/text1?serverTimezone=GMT";//数据库url
            String username = "root";//数据库账号
            String password = "QAZWSX564368";//数据库密码
            Connection connection=DriverManager.getConnection(url,username,password);

(3)连接数据库常见错误

1、未导入驱动包

 2、URL写错了

 3、账号密码错误

 4、驱动包版本不兼容

4、动态数据查询

 

package MYSQLStudy;

import java.sql.*;

public class Demo2 {
    static Statement statement;
    static ResultSet resultSet;
    static Connection connection;

    public static void main(String[] args) {
        try {
            Class.forName(("com.mysql.cj.jdbc.Driver"));
        String url="jdbc:mysql://127.0.0.1:3306/text1?serverTimezone=GMT";
        String username = "root";
        String password = "QAZWSX564368";
        connection= DriverManager.getConnection(url,username,password);

        String sql="select* from student where sname=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,"小明");
        resultSet=preparedStatement.executeQuery();
        if (resultSet.next()){
            System.out.println(resultSet.getInt(1)+resultSet.getString(2)+resultSet.getString(3)+resultSet.getString(4));
        }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

 

 String sql="select* from student where sname=? and sex=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,"小明");

如果引用两个通配符,却只是用了一个,就会报错

5、 增修删数据库

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小郭同学忒骚了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值