JavaWeb的學習可能要告一段落了,下一階段可能是學習框架...
這幾天也做完了使用三層架構重構Login項目,Entity實體類,使用類減少重複代碼的工作,就不貼出來了
關於增刪改users表的代碼
Utils
package com.DAL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class sqlUtils {
private String DBName;
private String UserName;
private String PassWord;
private String Host;
private Connection conn;
private PreparedStatement pst;
private ResultSet rs;
/**
* 通過參數的構造器傳入連接數據庫所需要的數據
*
* @param Host
* //主機的IP或者域名
* @param DBName
* //所連接數據庫的名稱
* @param UserName
* //數據庫的用戶名
* @param PassWord
* //數據庫的密碼
*/
public sqlUtils(String Host, String DBName, String UserName, String PassWord) {
super();
this.Host = Host;
this.DBName = DBName;
this.UserName = UserName;
this.PassWord = PassWord;
}
// 數據庫的連接方法
private void getConnection() {
String URL = "jdbc:mysql://" + Host + ":3306/" + DBName;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, UserName, PassWord);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 數據的查詢方法
*
* @param sql
* //查詢的SQL語句
* @return//返回ResultSet結果集
*/
public ResultSet executeQuery(String sql) {
try {
this.getConnection();
pst = conn.prepareStatement(sql);
return rs = pst.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 數據庫的更新方法--增加,修改,刪除
*
* @param sql
* //更新數據庫的SQL語句
* @return//返回一個整數,大於0說明更新成功
*/
public int executeUpdate(String sql) {
try {
this.getConnection();
pst = conn.prepareStatement(sql);
return pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
// 關閉數據庫連接
public void close() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
增
<%@ page language="java" import="java.util.*,java.sql.*,com.DAL.*"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>數據庫的add</title>
</head>
<body>
<!-- 提交的表單 -->
<form action="/MyWeb/DAL/Add.jsp" method="post">
增加的id <input name="id"><br>增加id的username <input
name="username"><br>增加id的password <input name="password">
<br> <input type="submit" value="提交">
</form>
<hr>
<a href="/MyWeb/index.jsp">返回</a>
<%
//獲取傳來的值
String id = request.getParameter("id");
String username = request.getParameter("username");
String password = request.getParameter("password");
//數據庫操作
String sql = "INSERT INTO `users` (`id`, `username`, `password`) VALUES ('"
+ id + "', '" + username + "', '" + password + "');";
sqlUtils su = new sqlUtils("localhost", "mybase", "root", "root");
su.executeUpdate(sql);
su.close();
%>
</body>
</html>
刪
<%@ page language="java" import="java.util.*,java.sql.*,com.DAL.*"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>數據庫的Delete</title>
</head>
<body>
<form action="/MyWeb/DAL/Delete.jsp" method="get">
要刪除的id <input name="id"><br> <input type="submit"
value="提交">
</form>
<hr>
<a href="/MyWeb/index.jsp">返回</a>
<%
String id = request.getParameter("id");
String sql = "delete from users where id=" + id + "";
sqlUtils su = new sqlUtils("localhost", "mybase", "root", "root");
su.executeUpdate(sql);
su.close();
%>
</body>
</html>
改
<%@ page language="java" import="java.util.*,java.sql.*,com.DAL.*"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>數據庫的Update</title>
</head>
<body>
<form action="/MyWeb/DAL/Update.jsp" method="post">
更改的id <input name="3"><br>username <input name="1"><br>password
<input name="2"> <br> <input type="submit" value="提交">
</form>
<hr>
<a href="/MyWeb/index.jsp">返回</a>
<%
String name = request.getParameter("1");
String passwd = request.getParameter("2");
String id = request.getParameter("3");
sqlUtils su = new sqlUtils("localhost", "mybase", "root", "root");
String sql = "UPDATE `users` SET `username`='" + name
+ "', `password`='" + passwd + "' WHERE (`id`='" + id
+ "');";
su.executeUpdate(sql);
su.close();
%>
</body>
</html>
效果如圖
登錄成功後纔可以進行操作