1.1 SQLhtml
建立數據庫java
create database db;
建立表mysql
create table userInfo( id int primary key , name varchar(20), password varchar(20), age int, email varchar(20) );
導入測試數據web
insert into userInfo values(1,'super','123',25,'123456@nova.com'); insert into userInfo values(2,'nova','111',26,'test@nova.com');
在SQLog或Navicat執行以上SQL語句以後,即完成了數據庫的基礎數據的預備sql
考慮在後續開發中,數據庫的路徑、用戶名、密碼等內容是可能會改變的,後續的開發中,應該儘可能的避免修改代碼,因此建立DBConfig.properties配置文件,將後續可能發生改變的內容放在配置文件中,後續只要修改配置文件便可。DBConfig.properties文件是以鍵值對的形式存儲的。數據庫
DBConfig.properties編程
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db
user=root
password=(這裏寫你的數據庫密碼)
在編程中,應該習慣於使用面向對象的思想,加強代碼的複用性、健壯性、可讀性。因此,這裏我將程序中對數據庫的操做封裝成一個工具類,而且內部的方法使用static修飾。app
用static修飾的好處在於, static修飾的方法被放在JVM的靜態方法區,隨着類的加載而加載,可以使用類名調用方法,而且static執行的效率高,速度快,因此,咱們封裝的工具類應儘可能使用靜態的,要注意的是,靜態的方法中,只能調用靜態的成員變量。ide
DBUtil.java工具
package com.nova.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ResourceBundle; /** * 數據庫工具類 * @author supernova * */ public class DBUtil { private static String url; private static String user; private static String pwd; //由於獲取文件配置信息和加載驅動只須要執行一次,因此將這兩個功能放入靜態代碼塊中,隨着類的加載而加載,能提升代碼的效率 static { //獲取配置文件信息 ResourceBundle bundle = ResourceBundle.getBundle("DBConfig"); url = bundle.getString("url"); user = bundle.getString("user"); pwd = bundle.getString("password"); //利用反射,加載驅動 try { Class.forName(bundle.getString("driver")); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //獲取鏈接通道 public static Connection getConnection() throws SQLException{ return DriverManager.getConnection(url,user,pwd); } //關閉全部資源 public static void closeAll(Connection connection,PreparedStatement pStmt, ResultSet resultSet) throws SQLException{ if(connection != null){ connection.close(); } if(pStmt != null){ pStmt.close(); } if(resultSet != null){ resultSet.close(); } } }
在用戶登陸中,每每會須要在客戶端顯示當前登陸用戶的信息,好比當前用戶名等,因此爲了增強程序的可擴展性,在服務端的JDBC編程中執行數據庫查詢操做時,應將查詢到的內容存到對象中,在後續須要使用用戶信息時,只需將該對象返回給客戶端便可,因此這就須要建立一個bean類,稱爲模型。bean類中成員變量的個數,類型應儘可能與數據庫表中的一致,以方便使用。
UserInfo.java
package com.nova.bean; public class UserInfo { private int id; private String name; private String password; private int age; private String email; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public UserInfo(int id, String name, String password, int age, String email) { super(); this.id = id; this.name = name; this.password = password; this.age = age; this.email = email; } public UserInfo() { super(); } @Override public String toString() { return "UserBean [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age + ", email=" + email + "]"; } }
ServerMain.java
package com.nova.test; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; 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 com.nova.bean.UserInfo; import com.nova.util.DBUtil; /** * 服務端主程序 * @author supernova * */ public class ServerMain extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //獲取客戶端發來的用戶名和密碼 String user = req.getParameter("user"); String pwd = req.getParameter("pwd"); //打印流 PrintWriter pw = resp.getWriter();; Connection con = null; PreparedStatement pStmt = null; ResultSet resultSet = null; UserInfo userInfo = null; try{ con = DBUtil.getConnection();//獲取數據庫鏈接, String sql = "select * from userInfo where name = ? and password = ?"; pStmt = con.prepareStatement(sql); pStmt.setString(1, user); pStmt.setString(2, pwd); resultSet = pStmt.executeQuery(); if(resultSet.next()){ userInfo = new UserInfo(); userInfo.setId(resultSet.getInt("id")); userInfo.setName(resultSet.getString("name")); userInfo.setPassword(resultSet.getString("password")); userInfo.setEmail(resultSet.getString("email")); } if(userInfo != null){ //登陸成功 pw.print("Login Success"); }else { //登陸失敗 pw.print("Login Failed"); } }catch(SQLException e){ e.printStackTrace(); }finally{ //最後須要把全部資源關閉 pw.close(); try { DBUtil.closeAll(con, pStmt, resultSet); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
下載mysql驅動jar包 下載連接:https://download.csdn.net/download/xin93/10334399
下載解壓後,將jar包複製到MyEclipse工程目錄的WebRoot/WEB-INF/lib中,並右鍵build path --> Add to build path
在網頁客戶端的body標籤中添加表單:
<form action="login"> username:<input type="text" name="user"/><br/> password:<input type="password" name="pwd"/><br/> <input type="submit" value="login"/> </form>
在WebRoot/WEB-INF的目錄下,打開web.xml文件,在web-app標籤下添加:
<servlet> <servlet-name>ServerMain</servlet-name> <servlet-class>com.nova.test.ServerMain</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServerMain</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
點擊Login按鈕,登陸成功。
輸入錯誤的用戶名密碼,
登陸失敗