面试机试当场用jsp+servlet写一个学生注册系统,将那个servlet附上分享

package com.ht.servlet;


import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Detail extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static String username = "liumei";
private static String password = "liumei";
public static Connection conn;

public ResultSet select(String str){
ResultSet rs = null;

try {

//第一步:在开发环境中加载驱动程序(向lib中添加ojdbc6.jar)

// 第二步:在Java程序中加载驱动程序
Class.forName("oracle.jdbc.driver.OracleDriver");
// 第三步:创建数据库连接对象
conn = DriverManager.getConnection(url, username,
password);
Statement statement = conn.createStatement();
rs = statement.executeQuery(str);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}

public List<StuTest> getBySno(String sno){
Detail detail = new Detail();
List<StuTest> list = null;
try {
ResultSet rs = detail
.select("select * from StuTest where StuTest.sno = '"
+ sno + "'");
if (rs.next()) {
list = new ArrayList<StuTest>();
do {
StuTest stutest = new StuTest();
stutest.setSno(rs.getString("sno"));
stutest.setName(rs.getString("name"));
stutest.setSex(rs.getString("sex"));
stutest.setCall(rs.getString("call"));
stutest.setPro(rs.getString("pro"));
list.add(stutest);
} while (rs.next());
}else{
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Detail detail = new Detail();
String sno = request.getParameter("sno");
String name = request.getParameter("name");
String call = request.getParameter("call");
String sex = request.getParameter("sex");
String pro = request.getParameter("pro");
String show = "show.jsp";
String dupname = "dupname.jsp";
request.setAttribute("sno", sno);

try {
//第一步:在开发环境中加载驱动程序(向lib中添加ojdbc6.jar)
//第二步:在Java程序中加载驱动程序
Class.forName("oracle.jdbc.driver.OracleDriver");
//第三步:创建数据库连接对象
conn = DriverManager.getConnection(url, username,
password);
//第四步:创建statement对象
Statement statement = conn.createStatement();
List<StuTest> list = detail.getBySno(sno);
if(list == null){
//第五步:使用statement对象执行sql操作
statement.execute("insert into StuTest " +
"values('"+sno+"', '"+name+"', '"+sex+"', '"+call+"', '"+pro+"')");
request.getRequestDispatcher(show).forward(request, response);
//response.sendRedirect(show);
}else{
request.getRequestDispatcher(dupname).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}

【JDBC也是这个机试题的重点,时间有限,还是没忘记加注释,将JDBC的五部曲写出来!这个Servlet是通过集成HttpServlet的方式来开发的,希望对正在找工作和在校学习的学生有用】

你可能感兴趣的:(servlet)