新手弱弱的问个问题,关于表单提交
不晓得这样写为什么不对啊,老是出错,郁闷啊。哥哥门指点下,小弟在此谢过。
前面还有个HTML页面,用于输入。
代码如下:
<%@ page language= "java " contentType= "text/html; charset=gbk "
pageEncoding= "gbk "%>
<%@ page import = "java.sql.* " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gbk ">
<title> Insert title here </title>
</head>
<body>
<%
String strUserNumber = request.getParameter( "txtUserNumber ");
String strUserPassword = request.getParameter( "txtUserPassword ");
String strUserName = request.getParameter( "txtUserName ");
String strSex = request.getParameter( "txtSex ");
String strAge = request.getParameter( "txtAge ");
String strDepartment = request.getParameter( "txtDepartment ");
String strSpecialty = request.getParameter( "txtSpecialty ");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = null;
try{
Class.forName( "com.mysql.jdbc.Driver ");
//连接
conn = DriverManager
.getConnection( "jdbc:mysql://localhost/mydata?user=root&password=root ");
//SQL语句
sql = "INSERT INTO user VALUES ( 'strUserNumber ', 'strUserPassword ', 'strUserName ', 'strSex ', 'strAge ', 'strDepartment ', 'strSpecialty ') ";
//创建数据集对象
stmt = conn.createStatement();
//执行SQL,返回一个记录到rs
rs = stmt.executeQuery(sql);
}catch(Exception e){
//e.printStackTrace();
out.println( "操作出错! ");
}finally{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
out.println( "数据库关闭失败! ");
}
}
%>
</body>
</html>
------解决方案--------------------首先 stmt.executeUpdate(sql);
其次出什么错了?
------解决方案--------------------sql错了吧。
sql = "INSERT INTO user VALUES ( 'strUserNumber ', 'strUserPassword ', 'strUserName ', 'strSex ', 'strAge ', 'strDepartment ', 'strSpecialty ') ";
应该这样写吧
sql = "INSERT INTO user VALUES ( ' " +strUserNumber+ " ', ' "+strUserPassword+ " ', ' "+.......
------解决方案--------------------《%
String usernumber=request.getParameter( "txtUserNumber ");
String password=request.getParameter( "txtUserPassword ");
String username=request.getParameter( "txtUsername ");
String sex=request.getParameter( "txtSex ");
String age=request.getParameter( "txtAge ");
String parent=request.getParameter( "txtDepartment "