1. 程式人生 > >#菜鳥新手EclipseJavaEE&MySQL&Tomcat#關於一個Eclipse中登陸介面通過連線MySQL資料庫進行驗證的小體驗(感謝qq群友引燃的解答讓我解決這個小問題)

#菜鳥新手EclipseJavaEE&MySQL&Tomcat#關於一個Eclipse中登陸介面通過連線MySQL資料庫進行驗證的小體驗(感謝qq群友引燃的解答讓我解決這個小問題)

原始碼:

package com.atguigu.javaweb;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; //大神解答!!! public class LoginServlet3 extends HttpServlet { /** * */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub
String username = req.getParameter("username"); String password = req.getParameter("password"); Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; PrintWriter out = resp.getWriter(); try { Class.forName("com.mysql.jdbc.Driver"
); String url = "jdbc:mysql://localhost:3306/atguigu?setUnicode=true&characterEncoding=utf8";//重點!!! String user = "root"; String password2 = ""; connection = DriverManager.getConnection(url,user,password2); String sql = "SELECT count(id) FROM tableData WHERE username = ? " + "AND password = ?";//tableData是表名!!!url中的atguigu是資料庫名!!!! statement = ((java.sql.Connection) connection).prepareStatement(sql); statement.setString(1, username); statement.setString(2, password); resultSet = statement.executeQuery(); if(resultSet.next()) { int count = resultSet.getInt(1); if(count > 0) { out.println("Hello: " + username); }else{ out.println("Sorry: " + username); } } } catch (Exception e) { e.printStackTrace(); }finally{ try { if(resultSet != null) { resultSet.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(statement != null) { statement.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(connection != null) { connection.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

這是經過改正後的程式碼:
String url="jdbc:mysql://localhost:3306/atguigu?setUnicode=true&characterEncoding=utf8";//重點!!!
改正前程式碼如下:
String url="jdbc:mysql://IP:3306/atguigu?user=root&password=&useUnicode="true";
其中IP忘記設定,沒有設定密碼的程式碼也不該如此。故而存在問題。

2. 我的引入包也存在問題,並且在這之前存在一個強制轉換。我的這段程式碼:
connection = DriverManager.getConnection(url,user,password2);

import java.sql.Connection; //大神解答!!!

改正前是connection = (Connection)DriverManager.getConnection(url,user,password2);
import com.sun.corba.se.pept.transport.Connection;

導致了相當嚴重的失誤。完全無法連線到資料庫。
3. 另外在連線到的相應資料庫的表時,也存在一些庫名及表名不對稱的問題。

這些程式碼:
String sql = "SELECT count(id) FROM tableData WHERE username = ? " + "AND password = ?"; //tableData是表名!!!url中的atguigu是資料庫名!!!!

改正前是這樣的:

String sql = "SELECT count(id) FROM users WHERE username = ? " + "AND password = ?"; 

很顯然,問題出在了sql語句中的FROM後面的users。這個是完全錯誤的!!!

***************在解決完以上的問題後,經過web.xml配置後就可以正常通過對比資料庫表中的username和password與登入介面的輸入資料是否一致進而做出相應的響應了。***************************BINGO!!!