读书人

看了好久都没看出异常在哪里请

发布时间: 2012-01-26 19:40:46 作者: rapoo

看了好久都没看出错误在哪里,请高手指点
/**
*
*/
package com.shop.conndb;

/**
* @author sunjiekun
*
*/
import javax.naming.*;
import javax.sql.DataSource;
import java.sql.*;
public class Condata {
private static Connection con;
ResultSet rs;
private static Context ctx;
private static Context env;
public static synchronized Connection getConnection() throws Exception{
try{
ctx=new InitialContext();
env=(Context)ctx.lookup( "java:comp/env ");
DataSource ds=(DataSource)env.lookup( "jdbc/sa ");
return con=ds.getConnection();
}
catch(SQLException e){
throw e;
}
catch(NamingException e){
throw e;
}

}


public ResultSet executeQuery(String sql){
try{
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException er){
System.err.print(er.getMessage());
}
return rs;
}


public int executeUpdate(String sql){
int result=0;
try{
Statement stmt=con.createStatement();
result=stmt.executeUpdate(sql);
}catch(SQLException eer){
System.out.print(eer.getMessage());
}
return result;
}


public int executeUpdate_id(String sql)
{
int result=0;
try{

Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
result=stmt.executeUpdate(sql);
String ID= "select @@IDENTITY as id ";
rs=stmt.executeQuery(ID);
if(rs.next())
{
int autoID=rs.getInt( "id ");
result=autoID;
}
}catch(SQLException ex)


{

result=0;
}
return result;
}

public void close(){
try{
if(con!=null){
con.close();
}
}catch(Exception err){
System.out.print(err);
}
try{
if(rs!=null){
rs.close();
}
}catch(Exception err){
System.out.print(err);
}
}
}


/**
*
*/
package com.shop.goods;
import com.shop.conndb.*;
import java.sql.*;
//import javax.sql.DataSource;
/**
* @author sunjiekun
*
*/
public class GoodsOperation implements GoodsInterface
{
private Condata con = new Condata();
private Connection conn;
chStr chStr = new chStr();
public GoodsOperation() throws Exception
{
conn=con.getConnection();

}


public int insert(goods g) {
int ret = -1;
try {
String sql = "Insert into tb_goods (TypeID,GoodsName,Introduce,Price,nowPrice,picture,newgoods,sale) values( " +
g.getTypeID() + ", ' " + chStr.chStr(g.getGoodsName()) + " ', ' " +
chStr.chStr(g.getIntroduce()) + " ', " + g.getPrice() + ", " +
g.getPrice() + ", ' " + chStr.chStr(g.getPicture()) + " ', " +
g.getNewGoods() + ", " + g.getSale() + ") ";

ret = conn.executeUpdate(sql);
}
catch (Exception e) {
ret = 0;
}
conn.close();
return ret;
}

public int update(goods g) {
int ret = -1;
try {
String sql = "update tb_Goods set TypeID= " +g.getTypeID() +


",GoodsName= ' " + chStr.chStr(g.getGoodsName()) + " ',introduce= ' " +
chStr.chStr(g.getIntroduce()) + " ',price= " + g.getPrice() +
",nowprice= " + g.getNowPrice() + ",picture= ' " + chStr.chStr(g.getPicture())+ " ',newgoods= " +
g.getNewGoods() + ",sale= " + g.getSale() + " where ID= " + g.getID();
ret = conn.executeUpdate(sql);
}
catch (Exception e) {
ret = 0;
}
conn.close();
return ret;
}


public int delete(goods g) {
int ret=-1;
try{
String sql= "Delete from tb_goods where ID= "+g.getID();
ret=conn.executeUpdate(sql);
}catch(Exception e){
ret=0;
}
conn.close();
return ret;

}


}


结果说没有为类型 Connection 定义方法 executeUpdate(String),是什么原因啊,不是已经定义好了吗

[解决办法]
里的values( " +少了 '符啊,
主,把你的insert句放到查分析器里面行一下就知道了.

读书人网 >Java Web开发

热点推荐