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

java JBDC重构 案例

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

java JBDC重构 案例

 StuentDao

public class StudentDao {
    public List select(){
        String sql2 = "select * from student";
        Execute execute = new Execute();
        ResultSet rs = execute.executeQuery(sql2);

        List stuList = new ArrayList<>();
        try {
            while (rs.next()) {
                System.out.println(rs.getInt("id"));
                StudentVO stu = new StudentVO();
                stu.setId(rs.getInt("id"));
                stu.setName(rs.getString("name"));
                stuList.add(stu);
            }
        } catch(SQLException se){
            throw new RuntimeException(se);
        } finally{
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return stuList;
    }

    public int insert(StudentVO stu){
        String sql = "insert into student values(" + stu.getId() + ",'" + stu.getName() + "',"
                + stu.getAge() + ",'" + stu.getAddress() + "','" + stu.getIdentify() + "','" + stu.getCountry() + "')";
        Execute execute = new Execute();
        int i = execute.executeUpdate(sql);
        return i;

    }
}

Execute :

import java.sql.*;

public class Execute {

    public int executeUpdate(String sql){
        PreparedStatement preparedStatement=null;
        try {
            preparedStatement =GetConnertion.con.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        int num = 0;
        try {
            num  = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return num;
    }
    public ResultSet executeQuery(String sql)  {
        PreparedStatement preparedStatement=null;

        try {
            preparedStatement =GetConnertion.con.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

            ResultSet rs = null;
        try {
            rs=preparedStatement.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return rs;

    }
}
GetConnertion类
public static Connection con = null;
        static{//找驱动
            try {
                Class.forName(JDBCLeading.DRIVER);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            try {
                DriverManager.getConnection(JDBCLeading.URL,JDBCLeading.USER,JDBCLeading.PASSWORD);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

JDBCLeading JBDC的properties
import com.jdbcPro.JDBCTest;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JDBCLeading {
    public static String DRIVER=null;
    public static String USER = null;
    public static String PASSWORD = null;
    public static String URL = null;
    public  JDBCLeading(){
        InputStream in = JDBCTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        DRIVER = properties.getProperty("Driver");
        URL=properties.getProperty("url");
        USER=properties.getProperty("user");
        PASSWORD =properties.getProperty("password");
    }

}

JBDC studentVO

public class StudentVO {
    @Override
    public String toString() {
        return "StudentVO{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                ", address='" + address + ''' +
                ", identify='" + identify + ''' +
                ", country='" + country + ''' +
                '}';
    }

    private int id;
    private String name;
    private int age;
    private String address;
    private String identify;
    private String country;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getIdentify() {
        return identify;
    }

    public void setIdentify(String identify) {
        this.identify = identify;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }




    public StudentVO() {
    }



    public StudentVO(int id, String name, int age, String address, String identify, String country) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
        this.identify = identify;
        this.country = country;
    }



测试类"

import com.jdbcPro.dao.StudentDao;
import com.jdbcPro.vo.StudentVO;

import java.util.List;

public class test01 {
    public static void main(String[] args) {

        StudentDao studentDao = new StudentDao();
        List list = studentDao.select();
        System.out.println(list.size());

        StudentVO stu = new StudentVO(338, "zhang", 16, "shandong", "asdas", "zhongguo");
        int num = studentDao.insert(stu);
        System.out.println(num);
    }
}

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

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

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