jdbc操作mysql

首先在mysql可视化工具中创建表,添加初始数据(可不加)。
在这里插入图片描述
在idea创建新模块,添加相关依赖。

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>

创建与数据库表对应的实体类。

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
    int eid;
    String ename;
    int age;
    int sex;
}

在resource中创建数据库属性文件 jdbc.properties。记得url中要加上时区。

jdbc.url=jdbc:mysql://localhost:3306/javase?useUnicode=true&useSSL=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.username=root
jdbc.password=123456

创建工具类,获取连接,注册驱动。

public class ConnectionInstance {
    public static Connection getConn() throws IOException, SQLException, ClassNotFoundException {
        //声明配置文件位置
        File file = new File("src/main/resources/dataSource.properties");
        FileInputStream fis = new FileInputStream(file);
        //新建properties加载文件
        Properties properties = new Properties();
        properties.load(fis);
        //通过properties创建连接
        String url = properties.getProperty("jdbc.url");
        String username = properties.getProperty("jdbc.username");
        String password = properties.getProperty("jdbc.password");
        String driver = properties.getProperty("jdbc.driver");
        //注册驱动
        Class.forName(driver);
        //获取连接
        Connection connection = DriverManager.getConnection(url, username, password);
        //返回连接
        return connection;
    }
}

创建测试类,进行测试。


/**
 * jdbc连接数据库查询
 */
public class JdbcTest {
    /**
     * 单个查询
     * @throws SQLException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    @Test
    public void test1() throws SQLException, IOException, ClassNotFoundException {
        Connection conn = ConnectionInstance.getConn();
        String sql="select * from emp where eid=?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setInt(1,3);
        //执行查询,返回一个结果集
        ResultSet resultSet = preparedStatement.executeQuery();
        Emp emp = new Emp();
        while (resultSet.next()){
            int anInt = resultSet.getInt(1);
            emp.setEid(anInt);
            String ename = resultSet.getString(2);
            emp.setEname(ename);
            int age=resultSet.getInt(3);
            emp.setAge(age);
            int sex = resultSet.getInt(4);
            emp.setSex(sex);
        }
        System.out.println(emp);
        if (resultSet!=null){
            resultSet.close();
        }
        if (preparedStatement!=null){
            preparedStatement.close();
        }
        if (conn!=null){
            conn.close();
        }
    }

    /**
     * 插入数据
     * @throws SQLException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    @Test
    public void test2() throws SQLException, IOException, ClassNotFoundException {
        Connection conn = ConnectionInstance.getConn();
        String sql="insert into emp values(?,?,?,?)";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setInt(1,6);
        preparedStatement.setString(2,"xw");
        preparedStatement.setInt(3,25);
        preparedStatement.setInt(4,1);
        preparedStatement.execute();
        if (preparedStatement != null) {
            preparedStatement.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

    /**
     * 批量查询
     * @throws SQLException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    @Test
    public void test3() throws SQLException, IOException, ClassNotFoundException {
        Connection conn = ConnectionInstance.getConn();
        String sql="select * from emp";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        ArrayList<Emp> list = new ArrayList<>();
        while(resultSet.next()){
            Emp emp = new Emp();
            int eid = resultSet.getInt(1);
            emp.setEid(eid);
            String ename = resultSet.getString(2);
            emp.setEname(ename);
            int age = resultSet.getInt(3);
            emp.setAge(age);
            int sex = resultSet.getInt(4);
            emp.setSex(sex);
            list.add(emp);
        }
        for (Emp emp:list) {
            System.out.println(emp);
        }
        if (ps != null) {
            ps.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值