当前位置:编程学习 > JAVA >>

j2ee 用MYSQL,不能插入数据,但是能删除和查询

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.domain.MyAccount;
import com.util.DBCon;

public class AccountDao {
    private Connection  conn;
    //查询所有帐务信息
    public  List  findAllAccount()
    {
     conn=DBCon.getConnection();
     String listSQL="select * from myaccount";
     List  list=new ArrayList();
     try
     {
     PreparedStatement  psmt=conn.prepareStatement(listSQL);
     ResultSet  rs=psmt.executeQuery();
     while(rs.next())
           {
         MyAccount   account=new  MyAccount();
         account.setId(rs.getInt(1));
         account.setIncomemoney(rs.getDouble(2));
         account.setCostmoney(rs.getDouble(3));
         account.setRecord(rs.getString(4));
         account.setTime(rs.getString(5));
         list.add(account);
           }
     conn.commit();
     return  list;
     }
     catch(Exception e)
     {
     e.printStackTrace();
     }
     finally
     {
     if(conn!=null)
     {
     try
     {
     conn.close();
     }
     catch(SQLException e)
     {
     e.printStackTrace();
     }
     }
     }
     return  list;
    
    }
    //添加帐务信息
   public   boolean  saveAllAccount(MyAccount account)throws Exception
    {
     conn=DBCon.getConnection();
     String  listSQLtj="insert  into  myaccount value(?,?,?,?)";
     PreparedStatement  psmt=conn.prepareStatement(listSQLtj);
     try
     {
    
     psmt.setDouble(1, account.getIncomemoney());
     psmt.setDouble(2, account.getCostmoney());  
     psmt.setString(3, account.getRecord());
     psmt.setString(4, account.getTime());
     psmt.executeUpdate();
     conn.commit();
     return true;
     }
     catch(SQLException e)
     {
     conn.rollback();
     e.printStackTrace();
     }
     return false;
    }
    //删除帐目
   public boolean  deleteSomeAccount(MyAccount account)throws Exception
   {
   conn=DBCon.getConnection();
   String  listSQL="delete  from  myaccount  where time=? ";
   PreparedStatement  psmt=conn.prepareStatement(listSQL);
   try
   {
   psmt.setString(1, account.getTime());    
   psmt.executeUpdate(); 
   conn.commit();
   return true;
   }
   catch(SQLException e)
   {
   conn.rollback();
   e.printStackTrace();
   }
   return false;
   }
   //计算帐目的和
   public  List  caculateAccount(int x,int y)
   {
   conn=DBCon.getConnection();
   double  incometotal=0.0;
   double  costtotal=0.0; 
   List  list2=new  ArrayList();
   String  listSQL1="select incomemoney from myaccount where id=?";
   String  listSQL2="select costmoney from myaccount  where id=?";
   try
   {
    for(int i=x;i<=y;i++)
     {    
   PreparedStatement  psmt1=conn.prepareStatement(listSQL1);
   psmt1.setInt(1, i);  
   ResultSet  rs1=psmt1.executeQuery();
   conn.commit();
  if(rs1.next())
  {
   MyAccount   account=new  MyAccount();
   account.setIncomemoney(rs1.getDouble(1));
       incometotal=incometotal+account.getIncomemoney();

   }
     }  
    for(int i=x;i<=y;i++)
     {
   PreparedStatement  psmt2=conn.prepareStatement(listSQL2);
   psmt2.setInt(1, i);    
   ResultSet  rs2=psmt2.executeQuery();
   conn.commit();
     if(rs2.next())
         {
     MyAccount   account=new  MyAccount();
     account.setCostmoney(rs2.getDouble(1));
     costtotal=costtotal+account.getCostmoney();  
  }      
     }
   list2.add(incometotal);
   list2.add(costtotal);
   conn.commit();
   return  list2;
   }
   catch(Exception e)
   {
   e.printStackTrace();
   }
   finally
       {
    if(conn!=null)
      {
    try
    {
    conn.close();
    }
    catch(SQLException e)
    {
    e.printStackTrace();
    }
      }
      }
  return  list2;
   }
}


--------------------编程问答-------------------- //添加帐务信息
   public   boolean  saveAllAccount(MyAccount account)throws Exception
    {
     conn=DBCon.getConnection();
     String  listSQLtj="insert  into  myaccount value(?,?,?,?)";
     PreparedStatement  psmt=conn.prepareStatement(listSQLtj);
     try
     {
    
      psmt.setDouble(1, account.getIncomemoney());
      psmt.setDouble(2, account.getCostmoney());  
      psmt.setString(3, account.getRecord());
      psmt.setString(4, account.getTime());
      psmt.executeUpdate();
      conn.commit();
      return true;
     }

你这里把你的sql加入到psmt.executeUpdate(sql);试试 --------------------编程问答-------------------- insert  into  myaccount value

我好想记得应该是values --------------------编程问答-------------------- 你这不报错不可能吧?

String  listSQLtj="insert  into  myaccount values(?,?,?,?)"; --------------------编程问答-------------------- value怎么进得去 values才对啊。
你这代码正常的话,调sava方法的时候应该抛异常才是 --------------------编程问答--------------------
引用 2 楼 coolbamboo2008 的回复:
insert  into  myaccount value

我好想记得应该是values


+1 --------------------编程问答--------------------
引用 2 楼 coolbamboo2008 的回复:
insert  into  myaccount value

我好想记得应该是values


加S是为了让你多插也行。 --------------------编程问答-------------------- 代码很不规范啊,finally中要关闭连接的,另外,注意一下语句的书写。有错误要根据错误提示去找原因,或者尝试改一下代码,尝试其他方式。很多时候程序的错误是由于自己粗心,犯得特别可笑的错误引起的。
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,