<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>插入学生信息</title>
</head>
<body>
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
String name=request.getParameter("name");
System.out.println(name);
String age=request.getParameter("age");
String sex=request.getParameter("sex");
String major=request.getParameter("major");
Connection conn=null;
Statement stat=null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;DatabaseName=student";
String user="sa";
String password="111";
conn=DriverManager.getConnection(url,user,password);
stat=conn.createStatement();
int rs=stat.executeUpdate("insert into stu(id,name,age,sex,major)values("+id+",'"+name+"',"+age+",'"+sex+"','"+major+"')");
%>
<center>
<br>
<a href=index.jsp>返回信息输入页面</a>
<a href=layout.jsp>进入信息查询页面</a>
</center>
<%
if(stat!=null)
{
stat.close();
}
if(conn!=null)
{
conn.close();
}
%>
</body>
</html>
就是以下这句: int rs=stat.executeUpdate("insert into stu(id,name,age,sex,major)values("+id+",'"+name+"',"+age+",'"+sex+"','"+major+"')");
------解决方案--------------------
看下 stat 创建成功没
------解决方案--------------------
你打印下那条SQL语句,就看到什么问题了
------解决方案--------------------
报什么错??
------解决方案--------------------
单双引号的问题
里面改为
"insert into stu(id,name,age,sex,major)values("+id+","+name+","+age+","+sex+","+major+")"试试看
------解决方案--------------------
报什么错??
------解决方案--------------------
可以考虑换为insert into stu(id,name,age,sex,major)values(?,?,?,?,?)的形式,
------解决方案--------------------
抛什么exception?
exception的stack trace是咋样的?
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>插入学生信息</title>
</head>
<body>
<%
try{
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
String name=request.getParameter("name");
System.out.println(name);
String age=request.getParameter("age");
String sex=request.getParameter("sex");
String major=request.getParameter("major");
Connection conn=null;
Statement stat=null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;DatabaseName=student";
String user="sa";
String password="111";
conn=DriverManager.getConnection(url,user,password);
stat=conn.createStatement();
int rs=stat.executeUpdate("insert into stu(id,name,age,sex,major)values("+id+",'"+name+"',"+age+",'"+sex+"','"+major+"')");
} catch (Exception e) {