2009-12-27 41 views
0
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)); 
    } 
} 

我已经修改了代码,这个....但仍然没有效果...查询成功运行,但在数据库中不添加数据。不知道你..?如果查询更改,代码将成功创建数据库中的表。问题在女士access..But代码数据插入运行正常

任何一个可以告诉我是什么问题或者我错了..

+0

你会得到什么错误信息? Employee表是如何定义的? – davek 2009-12-27 11:40:11

+0

表结构是雇员(empid,ename,ephone,email)都是ms访​​问的文本值..still不会插入值..但是如果qery就像创建表一样,它运行良好......它会创建表。 ..但没有插入值... – Sam 2009-12-27 12:07:06

+0

你为什么把这个wiki加入?它看起来非常具体,而不是讨论话题。 – duffymo 2009-12-27 12:57:18

回答

0

你不会看到任何的访问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); 
     } 
    } 
}