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;
        }

 

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