|
@@ -0,0 +1,99 @@
|
|
|
+package com.dao;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.DriverManager;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.sql.Statement;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import com.entity.Bar;
|
|
|
+public class BarDao {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public ArrayList<Bar> query(){ //这里的query()方法是将从数据库中读取到的数据存储到集合中
|
|
|
+ ResultSet rs = null;
|
|
|
+ Statement st = null;
|
|
|
+ ArrayList<Bar> barArr=new ArrayList<Bar>();
|
|
|
+ Connection conn = DataBaseUtils.getConnection();
|
|
|
+
|
|
|
+ try{
|
|
|
+
|
|
|
+ st = conn.createStatement();
|
|
|
+ rs = st.executeQuery("select * from testbar");
|
|
|
+ while(rs.next()){
|
|
|
+ Bar bar=new Bar();
|
|
|
+ bar.setName(rs.getString("name"));
|
|
|
+ bar.setNum(rs.getInt("num"));
|
|
|
+ barArr.add(bar); //将从数据库中读取到的数据 以bar对象的方式存储到 集合中
|
|
|
+ }
|
|
|
+ }catch(SQLException e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return barArr; //返回该集合
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存
|
|
|
+ * @param var1
|
|
|
+ */
|
|
|
+ public void save(String name,Integer num){
|
|
|
+ String sql = "insert into testbar(name,num) values(?,?)";
|
|
|
+ Connection conn = DataBaseUtils.getConnection();
|
|
|
+ try {
|
|
|
+ PreparedStatement pstmt = conn.prepareStatement(sql);
|
|
|
+ pstmt.setString(1, name);
|
|
|
+ pstmt.setInt(2, num);
|
|
|
+
|
|
|
+ pstmt.executeUpdate();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void update(String name,Integer num){ //该方法用于更新数据
|
|
|
+ try{
|
|
|
+ Connection conn = DataBaseUtils.getConnection();
|
|
|
+
|
|
|
+ PreparedStatement ptmt=conn.prepareStatement("update testbar set num=? where name=? ");
|
|
|
+ ptmt.setInt(1, num);
|
|
|
+ ptmt.setString(2,name);
|
|
|
+ ptmt.execute();
|
|
|
+ }catch(SQLException e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 根据name删除
|
|
|
+ * @param name
|
|
|
+ */
|
|
|
+public void del(String name){
|
|
|
+ String sql = "delete from testbar where name=?";
|
|
|
+ Connection conn = DataBaseUtils.getConnection();
|
|
|
+ try {
|
|
|
+ PreparedStatement pstmt = conn.prepareStatement(sql);
|
|
|
+ pstmt.setString(1, name);
|
|
|
+ pstmt.executeUpdate();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+}}
|
|
|
+
|
|
|
+
|