Jsp connection mysql database under MyEclipse to realize user login

jsp under MyEclipse connects to mysql database to realize user login


Tools: MyEclipese10.0 
      jdk1.8.x tomcat6.0  
      Mysql5.0.8
      mysql-connector-java-5.0.8-bin.jar


Purpose: Realize simple login
    
     1. Create a new web project in MyEclipse, then create a lib folder under this project, and then copy mysql-connector-java-5.0.8 -bin.jar to this folder, select the file, and then right-click Build Path—>Add Build Path. If you use jdbc, this operation is very important. It provides the driver for the jsp project under MyEclipse to connect to the Mysql database. This step is ignored in many blogs, which will cause errors when the driver cannot be found when connecting to the
     database
     .
     2. Write a form under the body tag in index.jsp to submit data, the code is as follows:
  <form action="LoginServlet.




      3. Create a new package under the src directory, name it com.sqldata.conn, and then create a new class, the code is as follows:
        public class sqldata {     public static Connection SQLConn(){      Connection conn=null;      String driver="com.mysql.jdbc.Driver";      String url = "jdbc:mysql://localhost:3306/javademo"; String      user="root";      String password="123456";      try { Class.forName(driver); conn=DriverManager.getConnection(url,user,password);  }   catch (ClassNotFoundException e) {    e.printStackTrace();  } catch (SQLException e) { e.printStackTrace    ();  }      return conn;    }     }


















4. Create a new getSqlData class to obtain data. The code is as follows:
public class GetSqlData {       public String getdata(String name){       String pwd="null";       Connection conn=sqldata.SQLConn();       try {         if(!conn.isClosed()){ System.out.println   ("Database connection succeeded");   Statement state=conn.createStatement() ;   String sql = "select * from user where name="+"'"+name+"'"; ResultSet rs = state.executeQuery(sql); if(rs.next()){ pwd=   rs.getString   (   "password");   }   else{   System.out.println("Username does not exist");   }             rs.close();         }         else{          conn.close();














  





        }
   }
   catch (SQLException e) {      e.printStackTrace();    }          return pwd;      } }    5. In this way, we have written a method to obtain data, and then we need to write a servlet to implement login detection, create a new servlet, the code is as follows: public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest           request                       st, HttpServletResponse response)        throws ServletException, IOException {           System.out.println("doGet()");           String username=request.getParameter("UserName");           String password=request.getParameter("PassWord");           System.out.println(username+":"+password);           Writer out=response.getWriter();












          

          

          
          String pwd=new GetSqlData().getdata(username);
          System.out.println(pwd);
          if(pwd.equals(password)){           out.write("true");           }           else{           out.write("false");           }            }             }        6. Create a database named javademo in Mysql, create a user table in the database, and the fields in the table are id, name , password, this is very simple.        7. Then publish it in MyEclipse, start the tomcat service, and run this project.







            

            

Guess you like

Origin blog.csdn.net/Yongjun_Ren/article/details/51525414