1:本地连接数据库的步骤
1 public class LoginDao { 2 3 // jdbc操作的五部曲 4 5 // 1: 导入jar包 6 // 2: 加载驱动 7 // 3: 获取连接 8 // 4: 预处理 9 // 5: 执行SQL 10 // 6: 处理结果集 11 // 7: 关闭连接 12 13 static String url = "jdbc:mysql://127.0.0.1:3306/week11"; 14 static String name = "root"; 15 static String pass = "root"; 16 17 public static void main(String[] args) { 18 19 try { 20 // 加载驱动 21 Class.forName("com.mysql.jdbc.Driver"); 22 // 获取连接 23 Connection connection = (Connection) DriverManager.getConnection(url, name, pass); 24 25 String sql = "select * from t_admin where aname=? and apass=?"; 26 // 预处理SQL语句 27 PreparedStatement ps = (PreparedStatement) connection.prepareStatement(sql); 28 29 // 设置预处理SQL的参数 30 ps.setObject(1, "dabu"); 31 ps.setObject(2, "123"); 32 33 // 执行查询 34 ResultSet rs = ps.executeQuery(); 35 36 // 如果查询到了数据 37 if (rs.next()) { 38 System.out.println("查询成功"); 39 } else { 40 System.err.println("查询失败"); 41 } 42 43 } catch (Exception e) { 44 45 e.printStackTrace(); 46 } 47 48 }
2:与web相结合
1 @WebServlet("/LoginServlet") 2 public class LoginServlet extends HttpServlet { 3 private static final long serialVersionUID = 1L; 4 5 public LoginServlet() { 6 // TODO Auto-generated constructor stub 7 } 8 9 protected void doGet(HttpServletRequest request, HttpServletResponse response) 10 throws ServletException, IOException { 11 // TODO Auto-generated method stub 12 response.getWriter().append("Served at: ").append(request.getContextPath()); 13 } 14 15 protected void doPost(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 // TODO Auto-generated method stub 18 doGet(request, response); 19 } 20 21 static String url = "jdbc:mysql://127.0.0.1:3306/week11?useUnicode=true&characterEncoding=utf-8&useSSL=false"; 22 static String name = "root"; 23 static String pass = "root"; 24 25 @Override 26 protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { 27 28 // 获取浏览器发过来的值 29 HttpServletRequest req = (HttpServletRequest) arg0; 30 31 //这个方法负责具体的实践 32 HttpServletResponse sr=(HttpServletResponse)arg1; 33 PrintWriter out=sr.getWriter(); 34 35 // 获取前台传过来的值 36 String userName = req.getParameter("username"); 37 String userPass = req.getParameter("userpass"); 38 39 // 去数据库里面查询有没有 40 // 2: 加载驱动 41 // 3: 获取连接 42 // 4: 预处理 43 // 5: 执行SQL 44 // 6: 处理结果集 45 // 7: 关闭连接 46 47 String sql = "select * from t_admin where aname=? and apass=?"; 48 49 Connection connection=null; 50 PreparedStatement ps=null; 51 ResultSet rs=null; 52 try { 53 54 //加载驱动 55 Class.forName("com.mysql.jdbc.Driver"); 56 //获取连接 57 connection = (Connection) DriverManager.getConnection(url, name, pass); 58 //预处理SQL 59 ps = (PreparedStatement) connection.prepareStatement(sql); 60 61 62 //设置从前台取出来的参数 63 ps.setString(1,"username"); 64 ps.setString(2,"userpass"); 65 //执行查询得到结果集 66 rs=ps.executeQuery(); 67 //判断结果 68 if(rs.next()){ 69 out.print("login success"); 70 }else { 71 out.print("login fail"); 72 } 73 74 } catch (Exception e) { 75 76 e.printStackTrace(); 77 }finally { 78 79 try { 80 rs.close(); 81 ps.close(); 82 connection.close(); 83 } catch (SQLException e) { 84 // TODO Auto-generated catch block 85 e.printStackTrace(); 86 } 87 88 } 89 }
login.jsp页面:
1 <body> 2 <!-- 3 action:当前表单提交的那个路径 4 method:当前表单提交的方式 5 name:对应我们的servlet去获取前台的值 6 7 --> 8 <form action="LoginServlet" method="get"> 9 10 用户名:<input name="username" type="text"/> 11 密码:<input name="userpass" type="password"/> 12 13 <!-- 默认是submit --> 14 <button>提交</button> 15 </form> 16 </body>
结果: