永发信息网

jsp 操作数据库插入操作没反应 只出现 "连接成功!" 求解决

答案:2  悬赏:60  手机版
解决时间 2021-12-24 14:16
  • 提问者网友:精神病院里
  • 2021-12-23 16:20
第一个页面代码:
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<body>
<table border="2">

<form action="doadd.jsp" method="post" name="form1">
<center><tr><td>ID</td><td><input type="text" name="ID"></td></tr>
<tr><td>Name</td><td><input type="text" name="name"/></td></tr>
<tr><td>Password</td><td><input type="password" name="password"/></td></tr>
<tr><td>Sex</td><td><input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女"/>女</td></tr>
<tr><td>Birthday</td><td><input type="text" name="birthday"/></td></tr>
<tr><td>Birthplace</td><td><input type="text" name="birthplace"/></td></tr><br/>
<tr><td><input type="submit" name="submit" value="提交">
<input type="reset" name="reset" value="重置"></td></tr>
</center>
</form>
</table>

</body>
</html>

第二个页面:
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<% response.setCharacterEncoding("gb2312");
String id = request.getParameter("id");
String name = request.getParameter("name");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String birthday = request.getParameter("birthday");
String brithplace = request.getParameter("birthplace");

String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=MYRS";
String sql = "insert into users values('"+id+"','"+name+"','"+password+"','"+sex+"','"+birthday+"','"+brithplace+"')";
String user = "sa";
String pwd = "123";
Connection conn = null;
Statement stmt = null;
//ResultSet rs = null;
try{
Class.forName(driver);
conn = DriverManager.getConnection(url,user,pwd);

//stmt = conn.prepareStatement(sql);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
out.print("连接成功!");
stmt.executeUpdate(sql);
out.print("操作成功!");
response.sendRedirect("query.jsp");
stmt.close();
conn.close();
}catch(Exception e){
e.getMessage();
}
//response.sendRedirect("query.jsp");
%>

</body></html>
最佳答案
  • 五星知识达人网友:傲气稳了全场
  • 2021-12-23 17:01
stmt = conn.createStatement();
out.print("连接成功!");
if (stmt.executeUpdate(sql)>=1){
out.print("操作成功!");
}else(
out.print("操作失败!");
}
stmt.close();
conn.close();

response.sendRedirect("query.jsp");
全部回答
  • 1楼网友:骨子里都是戏
  • 2021-12-23 18:27
我会继续学习,争取下次回答你
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯