求高手帮我用JSP技术写一个保存文章和发表文章的代码

2024-12-22 23:18:51
推荐回答(1个)
回答1:

发表文章:wenzhang.jsp:



















标题:
作者:
内容:



然后提交到另一个页面做处理:add.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>




保存文章



<%
String title = request.getParameter("title");
String writer = request.getParameter("writer");
String content = request.getParameter("content");
ResultSet rs=null;
str="insert into wenzhang(title,writer,content)values('"+title+"','"+writer+"','"+content+"');";
query.update(str);
out.println("成功发表文章!");
out.print("本页面将在3秒后自动跳转到上一页面!");
out.print("");
}
%>



数据库处理类:
dbfunction.java:
// 本类用来建立与数据库的连接
package wenzhang;
import java.sql.*;
public class dbfunction {
private static Connection con = null;
private static Statement stmt = null;
private static ResultSet rs = null;
public void connect(){
String url = "jdbc:sqlserver://localhost:1433/wenzhang"; 这里是数据库
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con= DriverManager.getConnection("jdbc:sqlserver://localhost:1433/wenzhang?user=sa&password=123456&useUnicode=true&characterEncoding=GB2312");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet select(String query)
{
System.out.println(query);
try {
stmt = con.createStatement();
rs = stmt.executeQuery(query);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public void update(String upstr)
{
System.out.println(upstr);
try {
stmt = con.createStatement();
int rs = stmt.executeUpdate(upstr);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public void close()
{
try {
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

就这些,不明白的可以联系我~~~