【问题标题】:Problem in data insertion in Ms access..But Code runs fineMs 访问中的数据插入问题..但代码运行良好
【发布时间】:2009-12-27 11:34:39
【问题描述】:
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));
    }
}

我已经将代码修改为这个......但仍然没有效果......查询运行成功但没有在数据库中添加数据。不知道你..?如果查询更改,代码会成功在数据库中创建表。

谁能告诉我是什么问题或者我哪里错了..

【问题讨论】:

  • 您会收到什么错误消息(如果有)? Employee表是如何定义的?
  • 表结构是 Employee(empid,ename,ephone,email) 都是 ms 访问中的文本值..仍然不插入值..但是如果 qery 就像创建表一样运行良好...它创建表...但未插入值...
  • 你为什么要把它做成一个wiki?看起来很具体,根本不是讨论话题。

标签: java ms-access jdbc


【解决方案1】:

在正确关闭连接之前,您不会看到任何带有 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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多