java web 對數據庫操作

說明:一般來說,做一個合格的java web項目,當用到對數據庫的操作時,建議把對數據庫的操作數據庫連接分開(數據庫連接工具類https://blog.csdn.net/quhongjuan12/article/details/103625296),在一個Dao 文件夾下,把對每一個數據庫中的數據表操作寫在一個文件中,例如,對用戶表的操作全寫在一個java文件中,對其他表的操作寫在Dao文件夾下其他java文件中。

package dao;

import entity.User;
import utils.DBUtils;

import java.sql.*;

public class UserDao {

        //給數據庫添加記錄
        public static void add(User u) throws SQLException {

        //因爲對數據庫查詢語句需要隨時value的值,所以使用PreparedStatement相對比較高效
        PreparedStatement prep;
        //數據庫查詢語句
        String sql = "INSERT INTO usertable(`name`,`password`,`email`) VALUE(?,?,?)";
        //獲取數據庫連接,這個類你可以去我的數據庫工具類找,上面有鏈接
        Connection connection = DBUtils.getConnection();
        prep = connection.prepareStatement(sql);
        //設置參數值,1,2,3分別是指問號的位置
        prep.setString(1, u.getUsername());
        prep.setString(2, u.getPassword());
        prep.setString(3, u.getEmail());
        //執行sql語句,請注意,不同Sql語句,執行函數不一樣
        prep.executeUpdate();
        //釋放數據庫連接,因爲對數據庫操作已經結束
        DBUtils.release(prep);

    }

    //通過用戶名查詢用戶,並返回,因爲這裏我的用戶不重名,所以只返回user,如果查詢結果有多條,使用resultSet,在後面介紹。
    public static User getByName(String name) throws SQLException {
        Connection connection = DBUtils.getConnection();
        PreparedStatement preparedStatement ;
        String sql = "SELECT * FROM usertable where name=?";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, name);
        ResultSet rs = preparedStatement.executeQuery();
        User u=new User();
        if(rs.next()){
            u.setUid(rs.getInt("id")); ;
            u.setUsername(rs.getString("name"));
            u.setPassword(rs.getString("password"));
            u.setEmail(rs.getString("email"));
            return u;
        }
        return null;
    }
    //更新操作,類似上面不重複講解
      public static void  updatePassword(String name,String password){
              Connection connection=DBUtils.getConnection();
              PreparedStatement preparedStatement;
              String sql="UPDATE usertable set password=? WHERE name=?";
          try {
              preparedStatement=connection.prepareStatement(sql);
              preparedStatement.setString(1,password);
              preparedStatement.setString(2,name);
              preparedStatement.executeUpdate();
              DBUtils.release(preparedStatement);
          } catch (SQLException e) {
              e.printStackTrace();
          }

      }

}

這裏介紹如果對於查詢結果有多條,該如何處理

 public static List<Topic> getByPid(int id) throws SQLException{
                Connection connection=DBUtils.getConnection();
                PreparedStatement preparedStatement;
                String sql="SELECT * FROM topic WHERE pid=?";
                //使用List來用來保存查出的多條記錄
                List<Topic> topics=new ArrayList<Topic>();
                preparedStatement=connection.prepareStatement(sql);
                preparedStatement.setInt(1,id);
                //查詢結果有多條,使用ResultSet 集保存
                ResultSet rs=preparedStatement.executeQuery();
                while(rs.next()){
                        Topic topic=new Topic();
                        topic.setPid(rs.getInt("pid"));
                        topic.setpText(rs.getString("ptext"));
                        topic.setpAuthor((rs.getString("pauthor")));
                        topic.setpTime(rs.getTimestamp("ptime"));
                        topic.setpCount(rs.getInt("pcount"));
                        //加到List鏈表裏
                        topics.add(topic);
                }
                return  topics;
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章