javaWeb簡單登陸頁面
1.程序配置的目錄
2.連接數據庫
1 package com.jaovo.msg.Util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class DBUtil { 10 11 public static Connection getConnection() { 12 try { 13 //1 加載驅動 14 Class.forName("com.mysql.jdbc.Driver").newInstance(); 15 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 16 // TODO Auto-generated catch block 17 e.printStackTrace(); 18 } 19 String user = "root"; 20 String password = "root"; 21 String url = "jdbc:mysql://localhost:3306/jaovo_msg"; 22 Connection connection = null; 23 try { 24 //2 建立連接對象connection 25 connection = DriverManager.getConnection(url,user,password); 26 } catch (SQLException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 return connection; 31 } 32 33 //關閉資源的方法 34 public static void close(Connection connection ) { 35 try { 36 if (connection != null) { 37 connection.close(); 38 } 39 40 } catch (SQLException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 } 45 public static void close(PreparedStatement preparedStatement ) { 46 try { 47 if (preparedStatement != null) { 48 preparedStatement.close(); 49 } 50 51 } catch (SQLException e) { 52 // TODO Auto-generated catch block 53 e.printStackTrace(); 54 } 55 } 56 public static void close(ResultSet resultSet ) { 57 try { 58 if (resultSet != null) { 59 resultSet.close(); 60 } 61 62 } catch (SQLException e) { 63 // TODO Auto-generated catch block 64 e.printStackTrace(); 65 } 66 } 67 68 }
錯誤總結:(1)原來不知道url語句的參數含義,因此直接複製了老師原來寫的uml值,致使很長時間鏈接不上數據庫。
url="jdbc:數據庫://端口名/即將鏈接的database"
(2)DriverManager.getConnection(url,"數據庫登陸名","密碼")。
(3)鏈接數據庫步驟
1.導入java.sql包
import java.sql.*;
2.加載數據庫的鏈接地址,用戶名和密碼。
Class.forName("com.mysql.jdbc.Driver") ;
3.定義數據庫的連接地址,用戶名和密碼。
String user = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3307/jaovo_msg";
4.獲得與數據庫的鏈接對象。
connection = DriverManager.getConnection(url,user,password);
5.聲明sql語句。
String sql = "select * from user2 ";
6.獲得語句對象。
preparedStatement = connection.prepareStatement(sql);
7.執行Sql語句。
resultSet = preparedStatement.executeQuery();
8.處理Sql語句的返回語句。
while(resultSet.next()) {
user = new User();
user.setName(resultSet.getString("user"));
user.setPassword(resultSet.getString("password"));
users.add(user);
}
9.關閉對象
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
3.登陸用戶類
1 package com.jaovo.msg.model; 2 3 public class User { 4 private int id; 5 private String username; 6 private String nickname; 7 private String password; 8 public int getId() { 9 return id; 10 } 11 public void setId(int id) { 12 this.id = id; 13 } 14 public String getUsername() { 15 return username; 16 } 17 public void setUsername(String username) { 18 this.username = username; 19 } 20 public String getNickname() { 21 return nickname; 22 } 23 public void setNickname(String nickname) { 24 this.nickname = nickname; 25 } 26 public String getPassword() { 27 return password; 28 } 29 public void setPassword(String password) { 30 this.password = password; 31 } 32 33 }
因爲這只是作一個簡單的用戶登陸頁面,因此只用填入登陸名和密碼。因此用戶類中只有name和password.
4.程序功能方法的接口。
1 package com.jaovo.msg.dao; 2 import com.jaovo.msg.model.User; 3 import java.util.List; 4 public interface IUserDao { 5 public void add(User user); 6 public void delete(int id); 7 public void update(User user); 8 public User load(int id); 9 public User load(String username); 10 public List<User> load(); 11 }
5.實現功能方法的類。
1 package com.jaovo.msg.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 11 import com.jaovo.msg.Util.DBUtil; 12 import com.jaovo.msg.Util.UserException; 13 import com.jaovo.msg.model.User; 14 15 import sun.net.www.content.text.plain; 16 17 public class UserDaoImpl implements IUserDao { 18 19 @Override 20 public void add(User user) { 21 //得到連接對象 22 System.out.println("bbbbbbbbbaaaaaa"+user); 23 Connection connection = DBUtil.getConnection(); 24 //準備sql語句 25 String sql = "select count(*) from t_user where username = ?"; 26 //建立語句傳輸對象 27 PreparedStatement preparedStatement = null; 28 ResultSet resultSet = null; 29 try { 30 preparedStatement = connection.prepareStatement(sql); 31 preparedStatement.setString(1, user.getUsername()); 32 //接收結果集 33 resultSet = preparedStatement.executeQuery(); 34 //遍歷結果集 35 while(resultSet.next()) { 36 if (resultSet.getInt(1) > 0) { 37 throw new UserException("用戶已存在") ; 38 } 39 } 40 41 sql = "insert into t_user(username,password,nickname) value (?,?,?)"; 42 preparedStatement = connection.prepareStatement(sql); 43 preparedStatement.setString(1, user.getUsername()); 44 preparedStatement.setString(2, user.getPassword()); 45 preparedStatement.setString(3, user.getNickname()); 46 preparedStatement.executeUpdate(); 47 } catch (SQLException e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 }finally { 51 //關閉資源 52 DBUtil.close(resultSet); 53 DBUtil.close(preparedStatement); 54 DBUtil.close(connection); 55 } 56 57 } 58 59 @Override 60 public void delete(int id) { 61 Connection connection = DBUtil.getConnection(); 62 String sql = "delete from t_user where id = ?"; 63 PreparedStatement preparedStatement = null; 64 65 try { 66 preparedStatement = connection.prepareStatement(sql); 67 preparedStatement.setInt(1, id); 68 preparedStatement.executeUpdate(); 69 } catch (SQLException e) { 70 // TODO Auto-generated catch block 71 e.printStackTrace(); 72 }finally { 73 DBUtil.close(preparedStatement); 74 DBUtil.close(connection); 75 } 76 77 } 78 79 @Override 80 public void update(User user) { 81 Connection connection = DBUtil.getConnection(); 82 //準備sql語句 83 String sql = "update t_user set password = ? , nickname=? where id = ?"; 84 //建立語句傳輸對象 85 PreparedStatement preparedStatement = null; 86 try { 87 preparedStatement = connection.prepareStatement(sql); 88 preparedStatement.setString(1, user.getPassword()); 89 preparedStatement.setString(2, user.getNickname()); 90 preparedStatement.setInt(3, user.getId()); 91 preparedStatement.executeUpdate(); 92 } catch (SQLException e) { 93 // TODO Auto-generated catch block 94 e.printStackTrace(); 95 }finally { 96 DBUtil.close(preparedStatement); 97 DBUtil.close(connection); 98 } 99 } 100 101 @Override 102 public User load(int id) { 103 Connection connection = DBUtil.getConnection(); 104 //準備sql語句 105 String sql = "select * from t_user where id = ?"; 106 //建立語句傳輸對象 107 PreparedStatement preparedStatement = null; 108 ResultSet resultSet = null; 109 User user = null; 110 try { 111 preparedStatement = connection.prepareStatement(sql); 112 preparedStatement.setInt(1, id); 113 resultSet = preparedStatement.executeQuery(); 114 while(resultSet.next()) { 115 user = new User(); 116 user.setId(id); 117 user.setUsername(resultSet.getString("username")); 118 user.setPassword(resultSet.getString("password")); 119 user.setNickname(resultSet.getString("nickname")); 120 } 121 } catch (SQLException e) { 122 // TODO Auto-generated catch block 123 e.printStackTrace(); 124 }finally { 125 DBUtil.close(resultSet); 126 DBUtil.close(preparedStatement); 127 DBUtil.close(connection); 128 } 129 return user; 130 } 131 132 @Override 133 public User load(String username) { 134 // TODO Auto-generated method stub 135 return null; 136 } 137 138 @Override 139 public List<User> load() { 140 Connection connection = DBUtil.getConnection(); 141 //準備sql語句 142 String sql = "select * from user2"; 143 //建立語句傳輸對象 144 PreparedStatement preparedStatement = null; 145 ResultSet resultSet = null; 146 //集合中只能放入user對象 147 List<User> users = new ArrayList<User>(); 148 User user = null; 149 try { 150 preparedStatement = connection.prepareStatement(sql); 151 resultSet = preparedStatement.executeQuery(); 152 while(resultSet.next()) { 153 user = new User(); 154 user.setId(resultSet.getInt("id")); 155 user.setUsername(resultSet.getString("username")); 156 user.setPassword(resultSet.getString("password")); 157 user.setNickname(resultSet.getString("nickname")); 158 users.add(user); 159 } 160 } catch (SQLException e) { 161 // TODO Auto-generated catch block 162 e.printStackTrace(); 163 }finally { 164 DBUtil.close(resultSet); 165 DBUtil.close(preparedStatement); 166 DBUtil.close(connection); 167 } 168 return users; 169 } 170 171 }
6.用戶登陸界面
1 <%@page import="com.jaovo.msg.Util.UserException"%>
2 <%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
3 <%@page import="com.jaovo.msg.model.User"%>
4 <%@ page language="java" contentType="text/html; charset=UTF-8"
5 pageEncoding="UTF-8"%>
6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
7 <html>
8 <%
9 //接收客戶端傳遞過來的參數
10 String username = request.getParameter("username");
11 String password = request.getParameter("password"); 12 String nickname = request.getParameter("nickname"); 13 if(username == null || "".equals(username.trim())){ 14 request.setAttribute("error", "用戶名不能爲空"); 15 } 16 %> 17 <jsp:forward page="addInput.jsp"></jsp:forward> 18 <% 19 20 System.out.println("222222"+username); 21 User user = new User(); 22 user.setUsername(username); 23 user.setPassword(password); 24 user.setNickname(nickname); 25 26 UserDaoImpl userDao = new UserDaoImpl(); 27 System.out.println("aabbccdd"+user.getUsername()); 28 try{ 29 userDao.add(user); 30 %> 31 32 33 用戶保存成功!!<br> 34 <a href="addInput.jsp">繼續添加</a><br> 35 <a href="#">用戶列表</a> 36 <% 37 }catch(UserException e){ 38 %> 39 <h2 style="color:red ; font-size:50px">發生錯誤 : <%=e.getMessage() %></h2> 40 <% 41 } 42 %> 43 </html> 44 <%@ page language="java" contentType="text/html; charset=UTF-8" 45 pageEncoding="UTF-8"%> 46 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 47 <html> 48 <head> 49 <title>用戶添加頁面</title> 50 </head> 51 <body> 52 <%=request.getAttribute("error") %> 53 <form action="add.jsp" method="get"> 54 <table align="center" border="1" width="500"> 55 <tr> 56 <td>用戶名稱 : </td> 57 <td> 58 <input type="text" name="username" /> 59 </td> 60 </tr> 61 <tr> 62 <td>用戶密碼:</td> 63 <td> 64 <input type="password" name="password" /> 65 </td> 66 </tr> 67 <tr> 68 <td>用戶暱稱:</td> 69 <td> 70 <input type="text" name="nickname" /> 71 </td> 72 </tr> 73 <tr align="center"> 74 <td colspan="2"> 75 <input type="submit" value="提交" /> 76 <input type="reset" value="重置" /> 77 </td> 78 </tr> 79 </table> 80 </form> 81 </body> 82 </html>
錯誤總結: (1)由於以前jsp的基本語法知識掌握較少,因此編寫的登陸界面較簡單。實現登陸界面的jsp文件的名爲doLogin.jsp,但因爲以前 <form >標籤中一直寫成dologin.jsp,因此一直不能呈貢跳轉頁面。
7.實現用戶登陸。
1 <%@page import="com.jaovo.msg.dao.UserDaoImpl" %> 2 <%@page import="com.jaovo.msg.model.User" %> 3 <%@page import="java.util.List"%> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 9 <% 10 //接收客戶端傳遞過來的參數 11 String username = request.getParameter("username"); 12 String password = request.getParameter("password"); 13 UserDaoImpl a=new UserDaoImpl(); 14 15 User user=a.load(username); 16 String password1=user.getPassword();//登陸窗口傳遞過來的密碼與數據庫中查詢到的密碼相比較 17 if(password.equals(password1)){ 18 out.println("登陸成功"); 19 } 20 else 21 out.println("輸入密碼或登陸名有錯!"); 22 23 24 %> 25 26 </html>
錯誤分析:在這個文件中要調用其餘文件中的內容,因爲其餘文件中的錯誤不少,因此在登陸界面跳轉以後,老是會報這個文件中語句的錯誤。
結果截圖:
經過此次學習發現作一個簡單的JavaWeb登陸界面就很吃力,中間遇到了不少的問題,也有編譯上的錯誤,也有不少借鑑百度上的東西,可是雖然到最後完成了,可是仍是花了很大的時間,還不符合一個真正編程者,之後須要大量的鍛鍊和思考,之後要更加耐得住寂寞,好好學習。只有耐得中寂寞,方可看的到繁華