在女士访问中插入数据时出现问题.但是代码运行正常 [英] Problem in data insertion in Ms access..But Code runs fine

查看:89
本文介绍了在女士访问中插入数据时出现问题.但是代码运行正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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));
    }
}

我已经对此进行了修改....但是仍然没有任何效果...查询成功运行,但未在数据库中添加数据.不知道y ..?如果查询更改,代码将成功在数据库中创建表.

I had modified the code to this....but still has no effect... The query runs successfully but doesn't add data in database. Don't know y..? The code creates the table in database successfully if query changed.

任何人都可以告诉我问题出在哪里还是我错了.

Can any one tell me what is the problem Or Where i m wrong..

推荐答案

在正确关闭连接之前,您将不会看到带有Access的任何INSERT.

You won't see any INSERT with Access until you close your Connection properly.

您的代码不会关闭任何资源,这肯定会给您带来痛苦.在finally块中以相反的顺序调用close方法.

Your code doesn't close any resources, which will surely bring you grief. Call the close methods in reverse order in a finally block.

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);
        }
    }
}

这篇关于在女士访问中插入数据时出现问题.但是代码运行正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆