Java Ms access中的数据插入问题..但代码运行正常
我已经修改了这个代码…但是仍然没有效果。。。查询已成功运行,但未在数据库中添加数据。我不知道你。。?如果查询发生更改,代码将在数据库中成功创建表Java Ms access中的数据插入问题..但代码运行正常,java,ms-access,jdbc,Java,Ms Access,Jdbc,我已经修改了这个代码…但是仍然没有效果。。。查询已成功运行,但未在数据库中添加数据。我不知道你。。?如果查询发生更改,代码将在数据库中成功创建表 有人能告诉我问题出在哪里吗?在正确关闭连接之前,您不会看到任何带有Access的插入 你的代码没有关闭任何资源,这肯定会给你带来痛苦。在finally块中按相反顺序调用close方法 import java.sql.*; // I think this is a poor abstraction public class NewConnection
有人能告诉我问题出在哪里吗?在正确关闭连接之前,您不会看到任何带有Access的插入 你的代码没有关闭任何资源,这肯定会给你带来痛苦。在finally块中按相反顺序调用close方法
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是如何定义的?表结构是Employeeempid、ename、ephone、email都是ms access中的文本值..仍然不插入值..但是如果qery类似于创建表,它运行良好…它创建表…但不插入值..为什么要将其设置为wiki?这似乎很具体,根本不是一个讨论的话题。
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);
}
}
}