您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用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連接數據庫并實現增刪改查,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。