苏州科技学院javaee期末考试试卷大题
- 格式:doc
- 大小:41.50 KB
- 文档页数:3
第二题
Input.java
public class Input extends HttpServlet {
private static final long serialVersionUID = 1L;
public Connection conn;
public PreparedStatement ps;
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("gbk");
String title = request.getParameter("title");
String authorname = request.getParameter("authorname");
String context = request.getParameter("context");
String keyword[] =request.getParameterValues("keyword");
String local = request.getParameter("local");
int row = 0;
try{Class.forName("com.mysql.jdbc.Driver");
String uri = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123";
conn = DriverManager.getConnection(uri,username,password);
String sql = "insert into
news(title,authorname,context,keyword,local)values(?,?,?,?,?);
ps = conn.prepareStatement(sql);
ps.setString(1, title);
ps.setString(2, authorname);
ps.setString(3, context);
String str = "";
for (int i=0;i
ps.setString(4,str);
ps.setString(5, local);
row = ps.executeUpdate();
if (row>=1){
response.sendRedirect("Success.jsp");}else{
response.sendRedirect("input.jsp");}
}catch (Exception e){ }
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
this.doGet(request, response); } }
实体类news配置文件
public class News {
private String title;
private String authorname ;
private String context ;
private String keyword[];
private String local;
public String getTitle() {
return title;}
public void setTitle(String title) {
this.title = title;}
。。。}
InputAction.java
private static final long serialVersionUID = 1L;
public Connection conn;
public PreparedStatement ps;
private News news;
public Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
String uri = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123";
conn =
DriverManager.getConnection(uri,username,password);
}catch (Exception e){
e.printStackTrace();
}
return conn;
}
protected int addNews(News news){
String sql = "insert into
news(title,authorname,context,keyword,local)values(?,?,?,?,?)"
;
try{
ps = new
InputAction().getConnection().prepareStatement(sql);
ps.setString(1, news.getTitle());
ps.setString(2, news.getAuthorname());
ps.setString(3, news.getContext());
String str = "";
for (int i=0;i
}
ps.setString(4,str);
ps.setString(5, news.getLocal());
row = ps.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}
return row;
}
public String execute()throws Exception{
row = new InputAction().addNews(news);
if (row>=1){
return SUCCESS;
}else{
return ERROR;
}
}
public News getNews() {
return news;
}
public void setNews(News news) {
this.news = news;
}
}
Action配置文件名:struts.xml