赞
踩
import java.sql.*;
import javax.naming.*;
一、 建立连接
一、
不过部分IDE会自动导入java.beans.statement包,导致Statement类和sql包中的Statement类冲突,删除自动生成的导入语句即可。
import java.sql.*; import java.io.*; import javax.naming.*; import javax.servlet.*; import javax.servlet.http.*; public class Login extends HttpServlet{ protected void doPost(HttpServletRequest req, HttpServletResponse resp) { try{ String username=req.getParameter("username"); String password=req.getParameter("password"); PrintWriter out=resp.getWriter(); //database content Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javalogin", "root", "root"); Statement stmt=conn.createStatement(); String sql="select userPass from user where userName='"+username+"'"; ResultSet set=stmt.executeQuery(sql); String dbpass=""; if(set.next()){ dbpass=set.getString(1); if(dbpass.equals(password)){ out.println("<script>window.οnlοad=function(){alert(\"login success!\");window.location='first'}</script>"); }else{ out.println("<p>wrong password!</p><a href='localhost:8080/review'>Click here then go back!</a>"); } }else{ out.println("<p>wrong username!</p>"); } }catch(Exception e){ try{ PrintWriter out=resp.getWriter(); out.println(e.toString()); }catch(Exception ee){ ee.printStackTrace(); } } } protected void doGet(HttpServletRequest req, HttpServletResponse resp) { doPost(req, resp); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。