android-web-Mysql連接-------Web服務器連接Mysql數據庫測試(實現登錄查詢功能)

Web服務器連接Mysql登錄查詢功能小測試

需要的工具

Mysql、Eclipse ide、tomcat(工程中需要的jar包:mysql-connector-java-8.0.19.jar)

因爲我的Mysql是8.0.19版本的所以下載是這個,如果是5開頭的版本,最好是下載5版本開頭的jar,博主就因爲這個的錯誤失敗很多次,其中網上很多的資源使用還會有錯誤。具體添加方法見博客link

需要jar資源的可以私我[email protected] | [email protected]

一、數據庫中的配置:見下圖。

數據庫名稱mydb 表:user 表中內容(username,password)
在這裏插入圖片描述

二、數據庫配置好後,開始寫程序

下面是建立工程的目錄
在這裏插入圖片描述
多餘的我就不說了,直接上代碼

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);
	}

}

Myservlet1.java中的內容:此部分功能是連接Mysql並查詢內容

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(首頁:登錄Mysql的用戶名密碼,成功跳轉到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(登錄數據表中存放的內容,功能就是查詢數據庫信息)

<%@ 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>

運行成功

1.index.jsp(輸入您的數據庫用戶名密碼)

在這裏插入圖片描述

2.成功後跳轉界面user_login.jsp(現在輸入user表中的存放內容)

在這裏插入圖片描述

3.成功返回信息(下面打印你輸入的信息及mysql中查詢的信息)

在這裏插入圖片描述

就此大功告成,前前後後進了一個又一個的坑,重複進相同的大坑,後續的完善代碼會陸續更新(增刪查改,界面設計等問題)

源代碼地址link

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章