栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

15-maven的java工程取mysql中的数据库

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

15-maven的java工程取mysql中的数据库

整体目录:

pro.xml文件:



    4.0.0

    org.example
    maven_mysql
    1.0-SNAPSHOT

    
        
            mysql
            mysql-connector-java
            5.1.6
            runtime
        
        
            junit
            junit
            4.12
        
    


ItemsDao接口:

package com.xurong.dao;

import com.xurong.domain.Items;

import java.util.List;


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

Items类:

package com.xurong.domain;


public class Items {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

ItemsDaoImpl:

package com.xurong.impl;

import com.xurong.dao.ItemsDao;
import com.xurong.domain.Items;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class ItemsDaoImpl implements ItemsDao {
    public List findAll() throws Exception{
        List list = new ArrayList();//

        
        //2.先获取contection对象
        Connection connection = null;
        //3.获取真正操作数据的对象
        PreparedStatement preparedStatement = null;
        //4.执行数据库查询操作
        ResultSet resultSet = null;


        try{
            //1.加载驱动类
            Class.forName("com.mysql.jdbc.Driver");

            connection = DriverManager.getConnection("jdbc:mysql:///shopping","password","password");//maven是数据库名
            preparedStatement = connection.prepareCall("select * from product");
            resultSet = preparedStatement.executeQuery();

            //把5.数据库的结果集转成java的List集合
//        List list = new ArrayList();
            while(resultSet.next()) {
                //以下3个步骤完成一个Items的实例化,并将其存入到List集合中
                Items items = new Items();
                items.setId(resultSet.getInt("id"));
                items.setName(resultSet.getString("name"));
                list.add(items);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {//使用完记得要把资源关掉,所以要使用异常捕捉
            connection.close();
            preparedStatement.close();
            resultSet.close();
        }

        return list;
    }
}

ItemsTest类:

package com.xurong.test;

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

import java.util.List;


public class ItemsTest {

    @Test
    public void findAll() throws Exception{
        ItemsDao itemsDao = new ItemsDaoImpl();
        List list = itemsDao.findAll();
        for(Items items:list) {
            System.out.println(items.getName());
        }
    }
}

运行测试类输出结果:

数据库中shopping表内容:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325338.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号