簡單粗暴JavaWeb-第四篇:通過資料庫實現使用者註冊、登入
阿新 • • 發佈:2019-02-13
現在我們通過jdbc進行資料庫操作,將第三步的登入功能升級為登入、註冊兩個功能,並且基於mysql資料庫,經過這一步,最最基本的工程就有雛形了,後面就可以根據需求進行優化,譬如使用mybatis(方便好多)等。
1、修改登入註冊介面
首先,改進首頁index.jsp,增加註冊功能,程式碼如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title> 首頁 </title> </head> <body> <form action="" method="post"> <input name="userName" type="text" /> <br/> <input name="password" type="password"/> <br/> <input id="submitButton" type="submit" value="登入" onclick="javascript:this.form.action='login.html'"/> <input id="registerButton" type="submit" value="註冊" onclick="javascript:this.form.action='register.html'"/> </form> </body> </html>
介面如圖
2、引入jdbc包及實現查詢和插入方法
引入jdbc包,在pom.xml檔案中新增相關依賴
<!--jdbc連結mysql的包--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
然後編寫jdbc的方法,主要有兩個
//適用於select操作 public ResultSet executeQuery(String sql){ Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ahellospringmvcdemodb","root","nopassword"); statement = connection.createStatement(); resultSet = statement.executeQuery(sql); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e){ e.printStackTrace(); } return resultSet; } //適用於insert、update、delete等操作 public int executeUpdate(String sql){ Connection connection = null; Statement statement = null; int result = -1; try{ Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ahellospringmvcdemodb","root","
nopassword
"); statement = connection.createStatement(); result = statement.executeUpdate(sql); return result; } catch(ClassNotFoundException e){ e.printStackTrace(); } catch (SQLException e){ e.printStackTrace(); } return result;}
關於jdbc執行的流程可以查詢其他相關資料,大體流程是對資料庫進行連線,然後執行sql獲得結果並處理,關閉連結。這裡為了方便,直接採用了ResultSet作為返回結果,正常使用的時候應該將其轉化為其他容器型別,如果在獲取ResultSet之後把資料庫連結關閉了,那這個ResultSet就失效了。另外這裡採用了一些硬編碼,只是為了方便~
3、實現登入、註冊的service方法
登入和註冊按鍵會提交不同的url請求,由不同controller進行控制,以註冊為例,實現過程的service如下:
public CheckRegisterEnum checkRegisterParam(String userName,String password){
StringBuilder sql = new StringBuilder("");
sql.append("select * from tbl_user_info where user_name=\'")
.append(userName)
.append("\'");
ResultSet resultSet = null;
JDBCServiceImpl jdbcService = new JDBCServiceImpl();
resultSet = jdbcService.executeQuery(sql.toString());
try {
if (resultSet.next()){
return CheckRegisterEnum.DUPLICATE_USERNAME;
}
else{
StringBuilder registerSql = new StringBuilder("");
registerSql.append("insert into tbl_user_info (user_name,password) value (\'")
.append(userName)
.append("\',\'")
.append(password)
.append("\')");
System.out.println(registerSql);
if (jdbcService.executeUpdate(registerSql.toString())==1){
return CheckRegisterEnum.REGISTER_SUCCESS;
}
}
} catch (SQLException e){
e.printStackTrace();
}
return CheckRegisterEnum.FAIL_SYS;
}
根據結果不同返回使用者名稱佔用、註冊成功等資訊,同時在資料庫中進行相應的更新。