java JBDC重构 案例

 StuentDao

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

        List<StudentVO> 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<StudentVO> 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);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值