zoukankan      html  css  js  c++  java
  • jdbc操作数据库

    public class LoginServlet extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
                String sql = "select * from t_user where username=? and password=?";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1, username);
                ps.setString(2, password);
                ResultSet rs = ps.executeQuery();
                UserInfo userInfo = null;
                while (rs.next()) {
                    userInfo = new UserInfo();
                    userInfo.setId(rs.getInt("id"));
                    userInfo.setUsername(rs.getString("username"));
                    userInfo.setPassword(rs.getString("password"));
                }
    
                resp.setContentType("text/html;charset=utf-8");
                if (userInfo != null) {
                    resp.getWriter().write("登录成功");
                } else {
                    resp.getWriter().write("登录失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
  • 相关阅读:
    hdu 4407 Sum 容斥+当前离线
    2014第11周二开发记
    2014第11周一word样式
    2014第10周日
    2014第10周六
    2014第10周杂想
    2014第10周四项目开发回顾提纲
    2014第10周三高效程序员的习惯
    2014第10周二程序员思维
    2014第10周一
  • 原文地址:https://www.cnblogs.com/yinchh/p/12380835.html
Copyright © 2011-2022 走看看