Ms访问中的数据插入问题..但是代码运行正常

时间:2009-12-27 11:34:39

标签: java ms-access jdbc

import java.sql.*;

// I think this is a poor abstraction
public class NewConnection {
/*very important: dont use statics for your Connection, Statement and Query objects,
 since they can and will be overriden by other Instances of your NewConnection.*/
    // There's no need at all for having class members here.  It's actually
    // a terrible idea, because none of these classes are thread-safe.
    private Connection con;
    private ResultSet rs;
    private Statement sm;
    // Better to pass these in.
    private final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
    private final String URL = "jdbc:odbc:Driver={Microsoft Access driver (*.mdb)};DBQ=E:\\db1.mdb;DriverID=22";
   //  private final String URL = "jdbc.odbc.Cooper_Dsn1";
    // Another poor idea.  Why not just pass this in to the query method?
    private static String query;
    int i;
    private void getConnection(){
        try {
            Class.forName(DRIVER);
            }
        catch(ClassNotFoundException e)
        // Less information that printing the stack trace.
        {System.out.println("Error ="+e);}
        try{
            System.out.println("Driver Connected");
            con=DriverManager.getConnection(URL,"","");
            System.out.println("Database Connected");
            sm=con.createStatement();
        }catch(SQLException e){
            System.out.println(e.getMessage());
        }
    }
    // lower case "execute" is the Java convention
    private int ExecuteUpdate(String query1)
    { try{
         System.out.println(query1);
           i=sm.executeUpdate(query1);
           con.commit();
      }catch(SQLException e){
          // No rollback in the event of a failure
          System.out.println(e.getMessage());
      }
      return i;
    }
    public int executeUpdate(String sql) throws SQLException 
    {
        System.out.println(sql);
          con.commit();  // What's this doing?  Incorrect
        return sm.executeUpdate(sql);
    }

    // Here's how I might write an update method.
    public static int update(Connection connection, String sql) 
    {
        assert connection != null && sql != null;

        int numRowsAffected = 0;
        PreparedStatement ps = null;

        connection.setAutoCommit(false);
        try
        {
            numRowsAffected = ps.execute(sql);
            connection.commit();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            DatabaseUtils.rollback(connection);  // Add this method.
            numRowsAffected = 0;
        }
        finally
        {
            DatabaseUtils.close(ps);
        }

        return numRowsAffected;
    }

    public static void main(String []args) throws SQLException{
    NewConnection n= new NewConnection();
    n.getConnection();
          query="insert into Employee(empid,ename,ephone,email) values('samr','sam','sa','aas');";
            System.out.println(n.ExecuteUpdate(query));
    }
}

我已将代码修改为此....但仍无效...查询成功运行但未在数据库中添加数据。不知道你..?如果查询更改,代码将在数据库中成功创建表。

任何人都可以告诉我这是什么问题或我错在哪里..

1 个答案:

答案 0 :(得分:0)

在正确关闭Connection之前,您不会看到任何带有Access的INSERT。

您的代码不会关闭任何资源,这肯定会让您感到悲伤。在finally块中以相反的顺序调用close方法。

public class DatabaseUtils
{
    public static Connection createConnection(String driver, String url, String username, String password) 
        throws ClassNotFoundException, SQLException
    {
        Class.forName(driver);

        return DriverManager.getConnection(url, username, password);
    }

    public static void close(Connection connection)
    {
        try
        {
            if (connection != null)
            {
                connection.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace(e);
        }
    }

    public static void close(Statement statement)
    {
        try
        {
            if (statement != null)
            {
                statement.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace(e);
        }
    }

    public static void close(ResultSet rs)
    {
        try
        {
            if (rs != null)
            {
                rs.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace(e);
        }
    }
}