Maven 获取mysql数据库

在这里插入图片描述

在这里插入图片描述
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.abc</groupId>
    <artifactId>maven_mysql</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

ItemsDao

import domain.Items;

import java.sql.SQLException;
import java.util.List;

public interface ItemsDao {
    public List<Items> findAll() throws Exception;
}

ItemsDaoImpl

import dao.ItemsDao;
import domain.Items;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ItemsDaoImpl implements ItemsDao {
    public List<Items> findAll() throws Exception {
        List<Items> list = new ArrayList<Items>();
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;
        try {
            //加载驱动类
            Class.forName("com.mysql.jdbc.Driver");

            //获取connection对象
            connection = DriverManager.getConnection("jdbc:mysql:///users?useUnicode=true&characterEncoding=utf8","root","123456");

            //获取真正操作数据库的对象
            pst = connection.prepareCall("select * from user");

            //执行数据库查询操作
            resultSet = pst.executeQuery();

            //把数据库结果集转为list集合

            while (resultSet.next()){
                Items it = new Items();
                it.setId(resultSet.getInt("id"));
                it.setUsername(resultSet.getString("username"));
                it.setPassword(resultSet.getString("password"));
                list.add(it);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            connection.close();
            pst.close();
            resultSet.close();
        }

        return list;

    }
}

Domain

public class Items {
    private Integer id;
    private String username;
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

ItemsTest

import dao.ItemsDao;
import dao.impl.ItemsDaoImpl;
import domain.Items;
import org.junit.Test;

import java.util.List;

public class ItemsTest {
    @Test
    public void findAll() throws Exception {
        ItemsDao dao = new ItemsDaoImpl();
        List<Items> list = dao.findAll();

        for (Items items : list) {
            System.out.println(items.getId());
            System.out.println(items.getUsername());
            System.out.println(items.getPassword());
        }
    };
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值