JDBC连接步骤

第一步:在项目中新建一个名称叫lib的文件夹,然后将下载的MySQL连接jar包存入到这个lib文件夹中并配置环境
    第二步:编写代码,连接数据库并操作数据库中某个表里的数据
①:基本操作

//1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2.获得连接
        String url = "jdbc:mysql:///java";
        String username = "root";
        String password = "root";

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

        //3.获取发送SQL语句对象
        Statement statement = connection.createStatement();

        //4.编写SQL语句
        String sql = "insert into student1 value(2,'lisi')";

        statement.executeUpdate(sql);

        //5.释放资源
        statement.close();
        connection.close();

②:防止SQL注入

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String userName = sc.nextLine();
        System.out.println("请输入密码:");
        String pwd = sc.nextLine();

        // 1、注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 2、获得连接
        String url = "jdbc:mysql://localhost:3306/java";
        String user = "root";
        String password = "123456";
        Connection connection = DriverManager
                .getConnection(url, user, password);

        // 3、获取发送SQL对象
        String sql = "select * from db_user where userName = ? and password=?;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
//		System.out.println(preparedStatement);

        // 4、绑定参数,有多少个?绑定多少个参数值
        preparedStatement.setString(1, userName);
        preparedStatement.setString(2, pwd);
//		System.out.println(preparedStatement);

        // 5、执行SQL语句,并处理结果
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            System.out.println("用户名和密码正确,登录成功");
        } else {
            System.out.println("用户名或密码错误,登录失败");
        }

        // 6、释放资源:与关闭流的方式一样,先开的后关,后开的先关
        resultSet.close();
        preparedStatement.close();
        connection.close();
        sc.close();

    }

③:编写一个DB工具类

import java.sql.*;

public class DBUtils {
    static {
        // 类加载,执行一次!
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //1.获取连接
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql:///java", "root", "root");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    // 2.释放资源
    public static void closeAll(Connection connection, Statement statement,
                                ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}

需求:

public static void main(String[] args) throws ClassNotFoundException, SQLException {

        /*
         * 需求:在控制台输入用户名和密码,根据输入的用户名和密码在数据库中查询,如果用户名和密码在数据库中,提示登录成功,反之提示登录失败
         */
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String userName = sc.nextLine();
        System.out.println("请输入密码:");
        String pwd = sc.nextLine();
        //1.注册驱动
        //Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DBUtils.getConnection();
        String sql = "SELECT * FROM db_user where user=? and pwd = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,userName);
        preparedStatement.setString(2,pwd);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            System.out.println("用户名和密码正确,登录成功");

        }else{
            System.out.println("用户名或密码错误,登录失败");

        }
        // 6、释放资源:与关闭流的方式一样,先开的后关,后开的先关
        DBUtils.closeAll(connection, preparedStatement, resultSet);
        sc.close();
    }

 ④:编写配置文件db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/java
username=root
password=123456

db工具类

public class DBUtils {
    private static final Properties PROPERTIES = new Properties();//PROPERTIES是存储配置文件的集合

    static {
        InputStream is = DBUtils.class.getResourceAsStream("/db.properties");

        try {
            PROPERTIES.load(is);
           Class.forName(PROPERTIES.getProperty("driver"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(PROPERTIES.getProperty("url"), PROPERTIES.getProperty("username"), PROPERTIES.getProperty("password"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}

需求:

public static void main(String[] args) throws ClassNotFoundException, SQLException {

        /*
         * 需求:在控制台输入用户名和密码,根据输入的用户名和密码在数据库中查询,如果用户名和密码在数据库中,提示登录成功,反之提示登录失败
         */
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String userName = sc.nextLine();
        System.out.println("请输入密码:");
        String pwd = sc.nextLine();
        //1.注册驱动
        //Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DBUtils.getConnection();
        String sql = "SELECT * FROM db_user where user=? and pwd = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,userName);
        preparedStatement.setString(2,pwd);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            System.out.println("用户名和密码正确,登录成功");

        }else{
            System.out.println("用户名或密码错误,登录失败");

        }
        // 6、释放资源:与关闭流的方式一样,先开的后关,后开的先关
        DBUtils.closeAll(connection, preparedStatement, resultSet);
        sc.close();
    }

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值