请问怎么在servlet里面写数据库insert代码?
这个是我写的~访问不到数据库希望大家帮我改下~~万分感谢!public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("name").trim();
String password = request.getParameter("password").trim();
String sql = "insert into userdata(name,password)values('"+name+"','"+password+"')";
Connection con = null;
try {
con = new ConnectDB().getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
out.write("Success");
//System.out.println("h");
}
out.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
这个是我写的~访问不到数据库希望大家帮我改下~~万分感谢!public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("name").trim();
String password = request.getParameter("password").trim();
String sql = "insert into userdata(name,password)values('"+name+"','"+password+"')";
Connection con = null;
try {
con = new ConnectDB().getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
out.write("Success");
//System.out.println("h");
}
out.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
解决方案 »
- iframe是什么东西?
- 那位大侠有 等高线追踪 源码,,谢谢了,
- 一个端口能否让输入和输出同时进行?
- 怎样把appletviewer显示的内容显示全(超过一屏的情况)?为什么appletviewer中可以显示的东东在浏览器里看不了?
- java中的小问题
- 请问:急如何实现Applet之间跨浏览器窗口的通信
- jb6好烦呀,请高手指点!(因是初来驾道分子不多,望高手不吝赐教!!!)
- 关于javamail的搜索问题,帮我搞定的送100分。(因为开始搞java又不太懂)
- 给分问题:如何展开JTree中的节点,内在细说……在线,解决即给分……
- 请问谁有关于义务兵逃跑的法律文件!急急急!!!(慎重提示:不是灌水!)
- 对于老贴:<for each循环的一个奇怪现象!>的一些看法!~
- 关于java网络的一个不知道问题在哪儿的问题。
try {
con = new ConnectDB().getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql); if (rs.next()) {
out.write( "Success ");
//System.out.println( "h ");
} 改成try {
con = new ConnectDB().getConnection();
Statement stmt = con.createStatement();
stmt.executeQuery(sql) if (stmt.executeUpdate(sql) > 0){
out.write( "Success ");
//System.out.println( "h ");
}
try {
con = new ConnectDB().getConnection();
Statement stmt = con.createStatement();
int n=stmt.executeUpdate(sql) ;//n是影响的行数
if (n > 0){
out.write( "Success ");
}
存取方法可以参考楼上朋友的
不同数据库连接字不同,还有驱动jar包是否有。
http://www.10zhizui.cn
servlet不该去做这些的.