Topic: 是不是SQL语句错了?!不解中... |
Print this page |
1.是不是SQL语句错了?!不解中... | Copy to clipboard |
Posted by: wind520sand Posted on: 2005-09-17 14:50 我想用网页输入数据然后保存在MYSQL的表中 <%@page import="wind.*"%> <%@page import="java.sql.*"%> <% String name = request.getParameter("name"); String pass = request.getParameter("pass"); ConDB con = new ConDB(); String inp = "insert into login values('"+name+"','"+pass+"')"; ResultSet rs = con.executesql(inp); %> 这是我的JSP代码.可以运行.但是在数据库中却没有新加的记录啊.我都不知道怎么回事啊.请大家帮帮我.谢谢了! ConDB.class是我的连接数据库的类.我测试过是OK的 name pass这两个变量是我想输入的数据 我还想多问一句.values('"+name+"') 为什么要这么多的引号啊? |
2.Re:是不是SQL语句错了?!不解中... [Re: wind520sand] | Copy to clipboard |
Posted by: chengbd Posted on: 2005-09-18 05:24 con.executesql(inp); 不知详情。 SQL中的字符串常量需要用单引号包围起来。 |
3.Re:是不是SQL语句错了?!不解中... [Re: wind520sand] | Copy to clipboard |
Posted by: wind520sand Posted on: 2005-09-18 09:29 这就是我的ConDB.java了,斑竹帮我看看我什么地方错的.谢谢了 package wind; import java.sql.*; public class ConDB { private String url = "jdbc:mysql://localhost:3306/first2?user=root&password=root"; private String driver = "com.mysql.jdbc.Driver"; private Connection con = null; private ResultSet rs = null; public ConDB() { try { Class.forName(driver).newInstance(); } catch (ClassNotFoundException ex) { System.out.println("Classnotfound");} catch (IllegalAccessException ex){System.out.println("");} catch (InstantiationException ex) { System.out.println("Instantiation"); } } public ResultSet executesql(String sql) { try { rs = null; con = null; con = DriverManager.getConnection(url); Statement state = con.createStatement(); rs = state.executeQuery(sql); } catch (SQLException ex ){System.out.println("SQLException");} return rs; } } |
4.Re:是不是SQL语句错了?!不解中... [Re: wind520sand] | Copy to clipboard |
Posted by: wind520sand Posted on: 2005-09-20 08:30 斑竹.有空帮帮看看啊 大家也出出主意啊 现在郁闷了很久了啊 |
5.Re:是不是SQL语句错了?!不解中... [Re: wind520sand] | Copy to clipboard |
Posted by: chengbd Posted on: 2005-09-20 09:02 最好给我个完整的web程序,这样看,实在困难。 |
6.Re:是不是SQL语句错了?!不解中... [Re: chengbd] | Copy to clipboard |
Posted by: why Posted on: 2005-09-20 12:00 use executeUpdate() for insert. I'm wondering if an exception would be thrown, or an error message be printed on the console. I really don't know, since I've never made such a mistake. |
7.Re:是不是SQL语句错了?!不解中... [Re: wind520sand] | Copy to clipboard |
Posted by: java菜鸟2005 Posted on: 2005-09-20 15:35 另外,字段名需要指定吧?难道这个表只有这两个字段? values('"+name+"') 为什么要这么多的引号——因为值需要用单引号括起来,而变量name不能在双引号之中 |
8.Re:是不是SQL语句错了?!不解中... [Re: wind520sand] | Copy to clipboard |
Posted by: wind520sand Posted on: 2005-09-20 18:16 我把我的程序传上来,请大家看看 我的表就是两个字段 呵呵 jsp.rar (57.84k) |
9.Re:是不是SQL语句错了?!不解中... [Re: wind520sand] | Copy to clipboard |
Posted by: dennisjl Posted on: 2005-09-22 09:40 我建议还是先找个正确的实例运行起来,然后在此基础上做修改吧! 再然后和你现在的代码比较一下,问题就解决啦 |
10.Re:是不是SQL语句错了?!不解中... [Re: wind520sand] | Copy to clipboard |
Posted by: 沙漠小鸟 Posted on: 2005-09-22 13:07 你的Teacher 表中的字段是什么 ? 为什么写成+name+,+pass+ String inp = "insert into login values('"+name+"','"+pass+"')"; 不解! |
11.Re:是不是SQL语句错了?!不解中... [Re: wind520sand] | Copy to clipboard |
Posted by: beyond1984 Posted on: 2005-09-23 10:32
|
12.Re:是不是SQL语句错了?!不解中... [Re: wind520sand] | Copy to clipboard |
Posted by: Ada1979 Posted on: 2005-09-23 19:07 你既然是要插入为什么要用query啊~! |
13.Re:是不是SQL语句错了?!不解中... [Re: wind520sand] | Copy to clipboard |
Posted by: wind520sand Posted on: 2005-09-28 09:18 昨天看了一本书,无意间在我的ConDB.java 加入了 public void insert (String sql) { try { Class.forName(driver).newInstance(); con = DriverManager.getConnection(url); Statement state = con.createStatement(); state.executeUpdate(sql); } catch (ClassNotFoundException ex) {System.out.println("Classnotfound");} catch (IllegalAccessException ex) {System.out.println("");} catch (InstantiationException ex) {System.out.println("Instantiation");} catch (SQLException ex){System.out.println("sqlerror");} } 就可以了,原来加入后,不能返回数据集啊.注意 我用的是VOID呵呵! 我想DELETE也不可以的.要用单独的一个方法写出来的. 总算搞顶了,谢谢大家这么的帮忙与照顾. 这个贴也结束了! |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |