android-web-Mysql connection ------- Web server Mysql database connection test (Login for realization of functions)

Web server connection Mysql query function to log Quiz

Tools needed

Mysql, Eclipse ide, tomcat (project required jar package: mysql-connector-java-8.0.19.jar)

Because my Mysql is 8.0.19 version of the download is this so, if it is the beginning of the version 5, it is best to download the first 5 version of jar, bloggers fail because of this mistake many times, which also use a lot of resources online there have been mistakes. Adding specific methods, see blog link

Jar private resources required can I [email protected] | [email protected]

A database configuration: See FIG.

Mydb database table name: user table contents (username, password)
Here Insert Picture Description

Second, after the database is configured to start writing programs

The following is a project to create a directory of
Here Insert Picture Description
excess I will not say, directly on the code

The content MyServlet.java

package cdu.nls.login;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class MyServlet
 */
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		
		 String ID = request.getParameter("ID"); 
	     String PW= request.getParameter("PW");
	 
	     boolean type=false;
	     response.setContentType("text/html; charset=UTF-8");
	     PrintWriter out = response.getWriter();    
	     try {
	    	 Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动,注册到管理器
	    	 String url = "jdbc:mysql://localhost:3306/mydb?serverTimezone= UTC&characterEncoding=utf-8";
	    	 Connection con = DriverManager.getConnection(url,ID,PW);
	    	 if(con != null) {
	    		 out.println("mysql connection successful!");
	    		 con.close();
	    		 request.getRequestDispatcher("user_login.jsp").forward(request,response);
	    		 System.out.print("login_mysql_successful\n");
	    		 out.print("haha");
	    		
	    	 }
	    	 else {
	    		 out.println("mysql connection failture!");
	    	 }
	    	 
	     }   catch(ClassNotFoundException e) {
	    	 e.printStackTrace();
	     }catch (SQLException e) {
	    	 e.printStackTrace();
	     }
		              out.println(type);
		              
		              out.flush();
		              out.close();
		              
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		          		doGet(request, response);
	}

}

The content Myservlet1.java: This feature is part of the connection and Mysql query

package cdu.nls.login;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;

@WebServlet("/MyServlet1")
public class MyServlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
     

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  String user2 = request.getParameter("username"); 
	  String pass2= request.getParameter("password");
	  boolean type= false;
	  PrintWriter out = response.getWriter(); 
	  java.sql.Statement stmt = null;//设置stmt全局变量
	  System.out.print("获取jsp界面的:");
	  System.out.print("  "+user2);
	  System.out.print("  ");
	  System.out.print("length:"+user2.length());
	  System.out.print("  "+pass2);
	  System.out.print("  ");
	  System.out.print("length:"+pass2.length());
	  
	  try
      {
		 Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动,注册到管理器
	     String url = "jdbc:mysql://localhost:3306/mydb?serverTimezone= UTC&characterEncoding=utf-8";
	     Connection con = DriverManager.getConnection(url,"root","123456");//此处
	     stmt = con.createStatement();//实例化Statement对象,用stmt接收
         String sql;//定义一个string类型的数据变量sql
         sql = "SELECT username, password FROM user";//用sql接收数据库语句
         ResultSet rs = stmt.executeQuery(sql); //执行sql
     
         // 展开结果集数据库
         while(rs.next()){
             // 通过字段检索
             String use1  = rs.getString("username");
             String pass1 = rs.getString("password");
      
             System.out.print("\nmysql中获取的内容:\nmysql_username: " + use1);
             System.out.print("  ");System.out.print("length:"+use1.length());
             System.out.print(" mysql_password: " + pass1);System.out.print("  ");
             System.out.print("length:"+pass1.length());
            if(use1.contentEquals(user2) &&pass1.contentEquals(pass2))
             { 
            	 type=true;
            	 System.out.print("\nlogin mysql successful!");
            	 out.print("\nlogin mysql successful!");
             }
  
         }
         // 完成后关闭
         rs.close();
         stmt.close();
         con.close();
	}
        catch(Exception ex)
       {
            ex.printStackTrace();
       }
        finally
        {
        //	con.Close();
            out.print(type);
            out.flush();
            out.close();
        }
	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}

index.jsp (Home: Log Mysql user name and password, the successful jump to user_login.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  <%@ page import = "java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>hello(此页登录您的数据库用户名和密码)</h1>
<form id="from1" action="MyServlet" method="post">
<table>
<tr><td>用户名</td><td><input type="text"  name="ID"></td></tr>
<tr><td>密码</td><td><input type="password"  name="PW"></td></tr>

<tr><td colspan = "3" align = "center"><input type = "submit" value = "login_mysql" id = "btnde1" onclick="del(id)"/></td></tr>


</table>
</form>

</body>
</html>

user_login.jsp (login data content stored in the table, the function is to query database information)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<td colspan = "3" align = "center">
<h1>user login(此页是您数据库中数据表中存的内容)</h1></td>
<form id="from1" action="MyServlet1" method="post">
<table>
<tr><td>username</td><td><input type="text"  name="username"></td></tr>
<tr><td>password</td><td><input type="password"  name="password"></td></tr>

<tr><td colspan = "3" align = "center"><input type = "submit" value = "login" id = "btnde2" onclick="del(id)"/></td></tr>

</table>
</form>
</body>
</html>

Successful operation

1.index.jsp (Enter your database user name and password)

Here Insert Picture Description

2. After a successful jump interface user_login.jsp (now enter the content stored in the user table)

Here Insert Picture Description

3. The successful return information (print information below and enter your mysql query)

Here Insert Picture Description

This done, back and forth one after another into a pit, repeated the same pit, the code will continue to improve subsequent updates (additions and deletions to change search, interface design, and so on)

Source address link

Published 14 original articles · won praise 23 · views 3287

Guess you like

Origin blog.csdn.net/sf9090/article/details/104847003