this.id=id; this.name=name; this.age=age; this.gender=gender; this.major=major; } public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public int getAge(){ return age; } public void setAge(int age){ this.age=age; } public String getGender(){ return gender; } public void setGender(String gender){ this.gender=gender; } public String getMajor(){ return major; } public void setMajor(String major){ this.major=major; } }
9,在src目录下添加另一个包dbservlet在该包中建立一个AllServlet类 完整的AllServlet.java代码如下 package dbservlet;
import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet;
import java.sql.SQLException; 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;
import bean.Page;
import bean.StudentInfo;
public class AllServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; //doPost方法 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding(\ response.setCharacterEncoding(\ String methodName=request.getParameter(\ int method=Integer.parseInt(methodName); try { switch(method) { case 0: insert(request,response); case 1:
difpage(request,response); break; case 2: delete(request,response); break; case 3: update(request,response); break;
case 4: update1(request,response); break; case 5: dispatch(request,response); break; } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //doGet方法 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
//数据库连接方法 public Connection connect() throws ClassNotFoundException, SQLException{ Connection conn=null; Class.forName(\ String url=\ String user=\ String password=\ conn=DriverManager.getConnection(url,user,password); return conn; } //关闭数据库资源 public void close(Statement stat,Connection conn) throws SQLException{ if(stat!=null){ stat.close(); } if(conn!=null){ conn.close(); } } //插入方法 public void insert(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException{
Connection conn=null; Statement stat=null; String id=request.getParameter(\
String name=request.getParameter(\ String age=request.getParameter(\
String gender=request.getParameter(\ String major=request.getParameter(\ conn=connect(); stat=conn.createStatement(); stat.execute(\into student(id,name,age,gender,major) values(\ close(stat,conn); }
//查询方法 public ArrayList
rs=stat.executeQuery(\ } if(id==\
rs=stat.executeQuery(\ } if(id!=\
rs=stat.executeQuery(\* from student where id=\and name='\ } while(rs.next()) { StudentInfo st=new StudentInfo(); st.setId(rs.getInt(\ st.setName(rs.getString(\ st.setAge(rs.getInt(\ st.setGender(rs.getString(\ st.setMajor(rs.getString(\ result.add(st);