Java使用jdbc连接MySQL(封装使用)

前提条件:使用的idea,需先导入jdbc jar包

一、连接步骤

  1. 加载驱动
  2. 用户信息和url
  3. 使用Connection连接数据库
  4. 使用Statement执行sql对象
  5. 执行的结果返回到ResultSet
  6. 释放连接

二、具体过程

1.创建db.propertise文件

文件中保存以下内容(2.用户信息和url)。其中username是连接数据库的名字,password是数据库密码,test是数据库名。

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=123456

2.创建工具包utils,utils下创建封装类(例如JdbcUtils),封装连接数据库必要的重复的东西

我们将连接步骤中1,3,6封装在了JdbcUtils类中,方便我们直接调用使用

package JDBC.utils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtils {

    private static String driver=null;
    private static String url=null;
    private static String username=null;
    private static String password=null;
    static {
        try {
            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");

            //1.加载驱动
            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 conn, Statement st, ResultSet rs) throws SQLException {
        if(rs!=null){
            rs.close();
        }
        if(st!=null){
            st.close();
        }
        if(conn!=null){
            conn.close();
        }
    }
}

3.创建测试类,测试是否能连接成功

package JDBC;

import JDBC.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestJdbc {
    public static void main(String[] args) throws SQLException {
        //Connection代表数据库
        Connection conn=null;
        //Statement执行sql的对象
        Statement st=null;
        //返回的结果集,结果集中封装了我们全部查询出来的结果
        ResultSet rs=null;
        try {
            conn = JdbcUtils.getConnection();//获取数据库连接
            st=conn.createStatement();//获取sql的执行对象
            String sql="select * from user";
            rs=st.executeQuery(sql); //查询完毕  //增、删、改使用st.executeUpdate()
            while (rs.next()){
                System.out.print(rs.getString("name")+',');
                System.out.print(rs.getString("password")+',');
                System.out.print(rs.getString("email")+',');
                System.out.println(rs.getString("birthday"));
                System.out.println("==========================");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st,rs);//释放空间
        }

    }
}


结果:
在这里插入图片描述

三、为防止SQL注入,使用prepareStatement

package JDBC;

import JDBC.utils.JdbcUtils;

import java.sql.*;

public class TestPreparedStatement {
    public static void main(String[] args) throws SQLException {
        //Connection代表数据库
        Connection conn=null;
        //Statement执行sql的对象
        PreparedStatement st=null;
        //返回的结果集,结果集中封装了我们全部查询出来的结果
        ResultSet rs=null;
        try {
            conn = JdbcUtils.getConnection();//获取数据库连接

            String sql="select * from user where id=?"; //编写SQL

            st=conn.prepareStatement(sql);//预编译

            st.setInt(1,1);//传递参数

            rs=st.executeQuery(); //查询完毕  //增、删、改使用st.executeUpdate()
            while (rs.next()){
                System.out.print(rs.getString("name")+',');
                System.out.print(rs.getString("password")+',');
                System.out.print(rs.getString("email")+',');
                System.out.println(rs.getString("birthday"));
                System.out.println("==========================");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st,rs);//释放空间
        }

    }
}

结果
在这里插入图片描述

结构

文件结构
在这里插入图片描述
数据库表的数据
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值