• java数据库操作


    DataSource.class文件

    package day1216zuoye.common;

    import java.sql.*;

    public class DataSource {
    protected String driver="com.mysql.jdbc.Driver";
    protected String url="jdbc:mysql://localhost:3306/student";
    private Connection con=null;
    private PreparedStatement ps=null;
    protected ResultSet res=null;
    public Connection getConnection() throws Exception{
    Class.forName(driver);
    return DriverManager.getConnection(url,"root","root");
    }
    public void execute(String sql) throws Exception{
    con=getConnection();
    ps=con.prepareStatement(sql);
    ps.execute();
    }
    public void execute(String sql,Object[] obj) throws Exception{
    con=getConnection();
    ps=con.prepareStatement(sql);
    int i=1;
    for (Object o:obj){
    ps.setObject(i,o);
    i++;
    }
    ps.execute();
    }
    public ResultSet query(String sql) throws Exception{
    con=getConnection();
    ps=con.prepareStatement(sql);
    return ps.executeQuery();
    }
    public ResultSet query(String sql,Object []obj) throws Exception{
    con=getConnection();
    ps=con.prepareStatement(sql);
    int i=1;
    for (Object o:obj){
    ps.setObject(i,o);
    i++;
    }
    return ps.executeQuery();
    }
    public void close(){
    try {
    if(res!=null){
    res.close();
    }
    if(ps!=null){
    ps.close();
    }
    if(con!=null){
    con.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }

    操作文件

    package day1216zuoye.dao;


    import day1216zuoye.common.DataSource;
    import day1216zuoye.entity.StudentEntity;

    import java.util.ArrayList;
    import java.util.List;

    public class StudentDao extends DataSource {
    public void studentAdd(StudentEntity student){
    String sql="insert into t_student2(sid, sname, sgender, sbirthday, sadd, stel) values (null,?,?,?,?,?)";
    Object[]obj={student.getSname(),student.getSgender(),student.getSbirthday(),student.getSadd(),student.getStel()};
    try {
    execute(sql,obj);
    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    close();
    }
    }
    public List<StudentEntity> studentSelect(){
    String sql="select * from t_student2";
    List<StudentEntity> students=new ArrayList<>();
    try {
    res=query(sql);
    while (res.next()){
    StudentEntity student=new StudentEntity();
    student.setSid(res.getInt("Sid"));
    student.setSname(res.getString("Sname"));
    student.setSgender(res.getString("Sgender"));
    student.setSbirthday(res.getDate("Sbirthday"));
    student.setSadd(res.getString("Sadd"));
    student.setStel(res.getString("Stel"));
    students.add(student);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    close();
    }
    return students;
    }public List<StudentEntity> studentSelect(Integer i){
    String sql="select * from t_student2 where sid="+i;
    List<StudentEntity> students=new ArrayList<>();
    try {
    res=query(sql);
    while (res.next()){
    StudentEntity student=new StudentEntity();
    student.setSid(res.getInt("Sid"));
    student.setSname(res.getString("Sname"));
    student.setSgender(res.getString("Sgender"));
    student.setSbirthday(res.getDate("Sbirthday"));
    student.setSadd(res.getString("Sadd"));
    student.setStel(res.getString("Stel"));
    students.add(student);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    close();
    }
    return students;
    }
    public void studentDelete(Integer i){
    String sql="delete from t_student2 where sid="+i;
    try {
    execute(sql);
    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    close();
    }
    }
    public void studentalter(StudentEntity s,Integer i){
    String sql="update t_student2 set sname=?,sgender=?,sbirthday=?,sadd=?,stel=? where sid="+i;
    Object[] obj={s.getSname(),s.getSgender(),s.getSbirthday(),s.getSadd(),s.getStel()};
    try {
    execute(sql,obj);
    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    close();
    }

    }
    }
     
  • 相关阅读:
    hadoop基础学习---数据管理策略
    hadoop基础学习---基本概念
    hadoop配置
    linux配置java环境
    Linux使用expect实现自动登录的脚本
    机器学习系列-寒小阳
    深度学习与计算机视觉系列-寒小阳
    深度学习与计算机视觉(12)_tensorflow实现基于深度学习的图像补全
    深度学习与计算机视觉(11)_基于deep learning的快速图像检索系统
    深度学习与计算机视觉系列(10)_细说卷积神经网络
  • 原文地址:https://www.cnblogs.com/pursue339/p/10612854.html
Copyright © 2020-2023  润新知