插入语句提交问题
<%
try{
String dbpath= "c:\\Tomcat 5.0\\webapps\\test\\WEB-INF\\lib\\data\\t_test.mdb ";
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ= "+dbpath;
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String strdoType=(String)request.getParameter( "doType ");
if (strdoType.equals( "insert "))
{
String s_xh= "002 ";
String s_name=request.getParameter( "s_name ");
String s_xb=request.getParameter( "s_xb ");
String s_km=request.getParameter( "s_km ");
String s_cj=request.getParameter( "s_cj ");
String sql= "insert into student(s_xh,s_name,s_xb,s_km,s_cj,s_user) vlues( ' "+s_xh+ " ', "+
" ' "+s_name+ ", ' "+s_xb+ " ', ' "+s_km+ " ', "+s_cj+ ") ";
ResultSet rs=stmt.executeQuery(sql);//这句有错?
rs.close();
stmt.close();
conn.close();
}
}
catch(Exception e)
{
out.print( "数据库连接错误!,错误信息如下: <br> ");
out.print(e.getMessage());
}
%>
问题:
ResultSet rs=stmt.executeQuery(sql);语句中,如果sql是查询语句,则该语句可以执行,如果是象上面一样是插入语句,则出错,请问插入语句应该怎么提交?
------解决方案--------------------= "insert into student(s_xh,s_name,s_xb,s_km,s_cj,s_user) vlues( ' "+s_xh+ " ', "+
" ' "+s_name+ ", ' "+s_xb+ " ', ' "+s_km+ " ', "+s_cj+ ") ";
這句麼?首先values 不是vlues,
其次,報的什麼錯啊?
------解决方案--------------------insert into student(s_xh,s_name,s_xb,s_km,s_cj,s_user) vlues( ' "+s_xh+ " ', "+
" ' "+s_name+ ", ' "+s_xb+ " ', ' "+s_km+ " ', "+s_cj+ ") ";
另外这里的字段名是六个,而你的字段值是五个
------解决方案--------------------executeQuery是执行查询时候用的
插入应该写成:
ResultSet rs=stmt.executeUpdate(sql);
------解决方案--------------------= "insert into student(s_xh,s_name,s_xb,s_km,s_cj,s_user) vlues(\ ' "+s_xh+ "\ ',\ ' "+s_name+ "\ ',\ ' "+s_xb+ "\ ',\ ' "+s_km+ "\ ',\ ' "+s_cj+ "\ ',\ ' "+s_user+ "