Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Servlet/JSP/JSF/JavaFX Script  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to topicflat modethreaded modego to previous topicgo to next topicgo to back
作者 是不是SQL语句错了?!不解中...
wind520sand





发贴: 36
于 2005-09-17 14:50 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我想用网页输入数据然后保存在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+"') 为什么要这么多的引号啊?




作者 Re:是不是SQL语句错了?!不解中... [Re:wind520sand]
chengbd



版主


发贴: 687
于 2005-09-18 05:24 user profilesend a private message to usersend email to chengbdreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
con.executesql(inp); 不知详情。
SQL中的字符串常量需要用单引号包围起来。



知难而进
作者 Re:是不是SQL语句错了?!不解中... [Re:wind520sand]
wind520sand





发贴: 36
于 2005-09-18 09:29 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这就是我的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;
}
}



作者 Re:是不是SQL语句错了?!不解中... [Re:wind520sand]
wind520sand





发贴: 36
于 2005-09-20 08:30 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
斑竹.有空帮帮看看啊
大家也出出主意啊
现在郁闷了很久了啊



作者 Re:是不是SQL语句错了?!不解中... [Re:wind520sand]
chengbd



版主


发贴: 687
于 2005-09-20 09:02 user profilesend a private message to usersend email to chengbdreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
最好给我个完整的web程序,这样看,实在困难。


知难而进
作者 Re:是不是SQL语句错了?!不解中... [Re:chengbd]
why

問題兒童

总版主


发贴: 4629
于 2005-09-20 12:00 user profilesend a private message to usersend email to whyreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.Smile



作者 Re:是不是SQL语句错了?!不解中... [Re:wind520sand]
java菜鸟2005





发贴: 64
于 2005-09-20 15:35 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
另外,字段名需要指定吧?难道这个表只有这两个字段?

values('"+name+"') 为什么要这么多的引号——因为值需要用单引号括起来,而变量name不能在双引号之中



除满,我信赖满停
作者 Re:是不是SQL语句错了?!不解中... [Re:wind520sand]
wind520sand





发贴: 36
于 2005-09-20 18:16 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我把我的程序传上来,请大家看看
我的表就是两个字段
呵呵

jsp.rar (57.84k)



作者 Re:是不是SQL语句错了?!不解中... [Re:wind520sand]
dennisjl





发贴: 53
于 2005-09-22 09:40 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我建议还是先找个正确的实例运行起来,然后在此基础上做修改吧!
再然后和你现在的代码比较一下,问题就解决啦



作者 Re:是不是SQL语句错了?!不解中... [Re:wind520sand]
沙漠小鸟





发贴: 51
于 2005-09-22 13:07 user profilesend a private message to usersend email to 沙漠小鸟reply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你的Teacher 表中的字段是什么 ?
为什么写成+name+,+pass+

String inp = "insert into login values('"+name+"','"+pass+"')";

不解!



作者 Re:是不是SQL语句错了?!不解中... [Re:wind520sand]
beyond1984





发贴: 16
于 2005-09-23 10:32 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

//底层UserHandle
...
public UserHandle(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thinAngry127.0.0.1:1521:Beyond";
String user = "beyond";
String psd = "beyond";
tableName = "USER";
cn = DriverManager.getConnection(url,user,psd);
stmt = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); //是不是这里要指明stmt的类别允许更新才行,我是这么做的,可以更新,另外通过stmt.executeQuery(sql)应该可以更新!!
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
public void addUser(String sql)throws Exception{
try{
stmt.executeQuery(sql);
}
catch(SQLException ex){
System.out.println("SQL statement is wrong!");
System.out.println(ex.toString());
throw ex;
}
}
}

//上层类DoSQL
...
public void addUser(){
UserHandle uh = new UserHandle();
String sql = "Insert Into USER Values('";
if(!checkIntegrality()){
return;
}
sql += (userID +"','"+ userName +"','"+ realName +"','"+ sex +"','"
+ address +"','"+ telephone +"','"+ mobile +"','"+ email+"')");
try{
uh.addUser(sql); //调用UserHandle.addUser();
}
catch(Exception ex){
errorInfo.addElement(ex.toString());
}
}

//我连的Oracle数据库,希望能给你点帮助,Good luck!


beyond1984 edited on 2005-09-23 10:42

作者 Re:是不是SQL语句错了?!不解中... [Re:wind520sand]
Ada1979





发贴: 1
于 2005-09-23 19:07 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你既然是要插入为什么要用query啊~!


作者 Re:是不是SQL语句错了?!不解中... [Re:wind520sand]
wind520sand





发贴: 36
于 2005-09-28 09:18 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
昨天看了一本书,无意间在我的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也不可以的.要用单独的一个方法写出来的.
总算搞顶了,谢谢大家这么的帮忙与照顾.
这个贴也结束了!




reply to topicflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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