jsp 动态合成sql语句

来源:百度知道 编辑:UC知道 时间:2024/05/11 15:30:33
String title=request.getParameter("title");
String author=request.getParameter("author");
String content=request.getParameter("content");
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ce)
{
out.println(ce.getMessage());
}
try
{
con=DriverManager.getConnection("jdbc:odbc:news");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT * FROM news" );
String sql="INSET INTO news(author,subject,content ) VALUES('"+author+"',"+"'"+title+"',"+"'"+content+"')";
stmt.executeUpdate(sql);
out.print("已将该文章插入新闻中");
}
catch(SQLException e)
{
System.out.println(e.getMessage());

你把

String sql="INSET INTO news(author,subject,content ) VALUES('"+author+"',"+"'"+title+"',"+"'"+content+"')";
改成
String sql="insert into news(author,subject,content) values('"+author+"','"+title+"','"+content+"')";

stmt.executeUpdate(sql);前一句
把sql打印出来!
然后看一下行不行,复制下来,到数据库中执行一次
就知道是哪里错了

String title=request.getParameter("title");
String author=request.getParameter("author");
String content=request.getParameter("content");
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ce)
{
out.println(ce.getMessage());
}
try
{
con=DriverManager.getConnection(&