使用Java连接数据库并实现增删改查

发布时间:2020-10-28 17:10:06 作者:Leah
来源:亿速云 阅读:221

本篇文章为大家展示了使用Java连接数据库并实现增删改查,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

自定义连接数据库的util类

package com.shuzf.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtil {
  // 定义驱动器类的路径
  private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
  // 定义用于连接数据库的URL
  private static final String URL = "jdbc:oracle:thin****l";
  // 定义用于访问数据库的用户名及密码
  private static final String USERNAME = "s****t";
  private static final String PASSWORD = "t***r";

  // 加载驱动器类
  static {
    try {
      Class.forName(DRIVER);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  // 定义获得连接的方法
  public static Connection getConnection() {
    Connection conn = null;
    ;
    try {
      conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return conn;
  }

  // 定义释放数据库资源的方法
  public static void destory(Connection con, Statement stat, ResultSet rs) {
    if (rs != null) {
      try {
        rs.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }

    if (stat != null) {
      try {
        stat.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }

    if (con != null) {
      try {
        con.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }

}

基本类

package com.shuzf.jdbc;

public class Student {
  private Integer Id;
  private String Name;
  private String Sex;
  private int Age;
   
  public Student() {
    super();
  }
  public Student(String name, String sex, int age) {
    Id = null;
    Name = name;
    Sex = sex;
    Age = age;
  }
  public Integer getId() {
    return Id;
  }
  public void setId(Integer id) {
    Id = id;
  }
  public String getName() {
    return Name;
  }
  public void setName(String name) {
    Name = name;
  }
  public String getSex() {
    return Sex;
  }
  public void setSex(String sex) {
    Sex = sex;
  }
  public int getAge() {
    return Age;
  }
  public void setAge(int age) {
    Age = age;
  }

}

增删改查

package com.shuzf.jdbc;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class JdbcTest {

  public int insert(Student student) {
    Connection conn = JDBCUtil.getConnection();
    int i = 0;
    PreparedStatement pst = null;
    String sql = "insert into students (Name,Sex,Age,Addtime) values(?,?,?,?)";
    try {
      pst = conn.prepareStatement(sql);
      pst.setString(1, student.getName());
      pst.setString(2, student.getSex());
      pst.setInt(3, student.getAge());
      pst.setDate(4, new Date(new java.util.Date().getTime()));
      i = pst.executeUpdate();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.destory(conn, pst, null);
    }
    return i;
  }

  public int update(Student student) {
    Connection conn = JDBCUtil.getConnection();
    int i = 0;
    PreparedStatement pst = null;
    String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'";
    try {
      pst = conn.prepareStatement(sql);
      i = pst.executeUpdate();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.destory(conn, pst, null);
    }
    return i;
  }

  public int delete(Student student) {
    Connection conn = JDBCUtil.getConnection();
    int i = 0;
    PreparedStatement pst = null;
    String sql = "delete from students where Name='" + student.getName() + "'";
    try {
      pst = conn.prepareStatement(sql);
      i = pst.executeUpdate();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.destory(conn, pst, null);
    }
    return i;
  }

  public ArrayList<Student> getStudent(String name) {
    Connection conn = JDBCUtil.getConnection();
    PreparedStatement pst = null;
    ResultSet rs = null;
    ArrayList<Student> students = new ArrayList<Student>();
    String sql = "select * from students where Name='" + name + "'";
    try {
      pst = conn.prepareStatement(sql);
      rs = pst.executeQuery();
      // int count = rs.getMetaData().getColumnCount();// 指示列数目的 int值
      while (rs.next()) {
        Student s = new Student();
        s.setId(rs.getInt("id"));
        s.setName(rs.getString("name"));
        s.setSex(rs.getString("sex"));
        s.setAge(rs.getInt("age"));
        students.add(s);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.destory(conn, pst, rs);
    }
    return students;
  }

  public static void main(String[] args) {
    // TODO Auto-generated method stub

  }

}

上述内容就是使用Java连接数据库并实现增删改查,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. java连接数据库的步骤
  2. 使用python3.6怎么连接mysql数据库并实现增删改查操作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java 数据库 ava

上一篇:Java 实现将List中的实体类按照某个字段进行分组并存放至Map中操作

下一篇:Java实现动态代理的两种方式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》