?? updatingservlet.java
字號(hào):
package dcfs;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class for Servlet: UpdatingServlet
*
*/
public class UpdatingServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
private static final long serialVersionUID = 1L;
public UpdatingServlet() {
super();
}
public void destroy() {
super.destroy();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 從修改頁(yè)面的表單中獲得用戶修改后的信息,并將這些信息存入對(duì)應(yīng)的變量中,以便執(zhí)行數(shù)據(jù)操作
String id = request.getParameter("id");
String password = request.getParameter("password");
String username = new String(request.getParameter("username").getBytes(
"ISO-8859-1"), "GB2312");
String sex = request.getParameter("sex");
String school = new String(request.getParameter("school").getBytes(
"ISO-8859-1"), "GB2312");
String major = request.getParameter("major");
major = new String(major.getBytes("ISO-8859-1"), "GB2312");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String hobbies = request.getParameter("興趣");
String address = new String(request.getParameter("address").getBytes(
"ISO-8859-1"), "GB2312");
String introduce = request.getParameter("introduce");
introduce = new String(introduce.getBytes("ISO-8859-1"), "GB2312");
// 設(shè)置頁(yè)面的屬性
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<head>");
out.println("<title>信息修改成功</title>");
out.println("</head>");
out.println("<body>");
// 聲明數(shù)據(jù)庫(kù)連接的相關(guān)對(duì)象
Connection con;
Statement sql_update;
ResultSet rs;
// 定義數(shù)據(jù)庫(kù)更新操作語(yǔ)句
String stmt = "insert into user_info(ID號(hào),密碼,姓名,性別,學(xué)校,專業(yè),電話,郵箱,興趣,住址,自我簡(jiǎn)介,上次登錄時(shí)間,登錄次數(shù)) values('"
+ id
+ "','"
+ password
+ "','"
+ username
+ "','"
+ sex
+ "','"
+ school
+ "','"
+ major
+ "','"
+ phone
+ "','"
+ email
+ "','"
+ hobbies
+ "','"
+ address
+ "','"
+ introduce
+ "',sysdate,1)";
// 加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFound~~~~~~");
}
// 建立與數(shù)據(jù)庫(kù)的連接
try {
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "hongshuai",
"hongshuai");
System.out.println("數(shù)據(jù)庫(kù)連接成功!");
sql_select = con.createStatement();
sql = con.createStatement();
// 執(zhí)行數(shù)據(jù)庫(kù)SQL語(yǔ)句,查詢數(shù)據(jù)庫(kù)中是否為相同的id號(hào)用戶
rs = sql_select.executeQuery(stmt_select);
if (rs.next()) {
String id_db = rs.getString("ID號(hào)");
if (id.equals(id_db)) {
out.print("該用ID號(hào)已經(jīng)被注冊(cè),請(qǐng)換用其他的ID號(hào)!!<br>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
} else {
// 執(zhí)行數(shù)據(jù)庫(kù)SQL語(yǔ)句,向數(shù)據(jù)庫(kù)中插入注冊(cè)用戶的信息
sql.execute(stmt);
out.print("祝賀你已經(jīng)注冊(cè)<br>");
out.print("你的ID號(hào)為:" + id + "<br>");
out.print("你的密碼為:" + password + "<br>");
out.println("</body>");
out.println("</html>");
out.close();
}
// 關(guān)閉數(shù)據(jù)庫(kù)連接
con.close();
System.out.println("數(shù)據(jù)庫(kù)已經(jīng)關(guān)閉!~~~");
} catch (SQLException ex) {
System.out.println("SQL,注冊(cè)出錯(cuò)!");
}
}
public void init() throws ServletException {
super.init();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -