将数据插入Access数据库 [英] Inserting data into a Access Database

查看:102
本文介绍了将数据插入Access数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Access使用Access数据库进行Java操作时遇到问题,想知道是否有人可以对此进行说明



在我的主类中这样的电话

 产品p =新产品(test,Test Product,49.50); 
insertProduct(p);

这会调用insertProduct方法。我想做的是在数据库中插入一些测试数据,这里是我的代码。

  public static void insertProduct (产品p)
{
try
{
System.out.println(Insert test:);

String insertProduct =INSERT INTO Products(ProductCode,Description,Price)VALUES('andr','Beginning Android','38 .99');
PreparedStatement ps = connection.prepareStatement(insertProduct);
ps.setString(1,p.getCode());
ps.setString(2,p.getDescription());
ps.setDouble(3,p.getPrice());
ps.executeUpdate();

ResultSet rs = ps.executeQuery();

rs.next();
String productCode = rs.getString(1);
String description = rs.getString(2);
double price = rs.getDouble(3);

p = new Product(productCode,description,price);


printProduct(p);
System.out.println();

}
catch(SQLException e)
{
e.printStackTrace();
}
}



当我运行这个时我得到一个


线程main中的异常java.lang.NullPointerException


并且无法弄清楚发生了什么,我知道当我运行调试器和步骤的方法,它崩溃在这个语句的开始

  ps.setString(1,p.getCode()); 
ps.setString(2,p.getDescription());
ps.setDouble(3,p.getPrice());
ps.executeUpdate();

这里是堆栈跟踪的结果

  at sun.jdbc.odbc.JdbcOdbcPreparedStatement.clearParameter(JdbcOdbcPreparedStatement.java:1023)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setChar(JdbcOdbcPreparedStatement.java:3057)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setString(JdbcOdbcPreparedStatement.java:766)
在DBTesterApp.insertProduct(DBTesterApp.java:165)
在DBTesterApp.main(DBTesterApp.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl。 java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)


解决方案

堆栈跟踪中的行:

 在sun.jdbc.odbc.JdbcOdbcPreparedStatement.setString(JdbcOdbcPreparedStatement.java:766)
pre>

表示在调用PreparedStatement.setString()期间,它指示p.getCode()或p.getDescription()返回null。 / p>

因此,您的Product类有一个问题。您是否在构造函数中正确设置这些字段?



修正此问题后,您可能会失望地发现您插入的所有行具有完全相同的数据。



查看SQL以找出原因:

  String insertProduct =INSERT INTO Products(ProductCode,Description,Price)VALUES('andr','Beginning Android','38 .99'); 

这应该是:

  String insertProduct =INSERT INTO Products(ProductCode,Description,Price)VALUES(?,?,?); 


I am having trouble with a exercise I am working on with Java using a Access Database, wondering if anyone could shed some light on this

In my main class I make a call like this

Product p = new Product("test", "Test Product", 49.50);
insertProduct(p);

This calls the insertProduct(p); method. What I am trying to do is insert some test data into the database, here is my code for that.

public static void insertProduct(Product p)
{
    try
    {
        System.out.println("Insert test: ");

        String insertProduct = "INSERT INTO Products (ProductCode, Description, Price) VALUES ('andr', 'Beginning Android', '38.99')";
        PreparedStatement ps = connection.prepareStatement(insertProduct);
        ps.setString(1, p.getCode());
        ps.setString(2, p.getDescription());
        ps.setDouble(3, p.getPrice());
        ps.executeUpdate();

        ResultSet rs = ps.executeQuery();

        rs.next();
        String productCode = rs.getString(1);
        String description = rs.getString(2);
        double price = rs.getDouble(3);

        p = new Product(productCode, description, price);


        printProduct(p);
        System.out.println();

    }
    catch(SQLException e)
    {
        e.printStackTrace();
    }
}

when I run this I get a

Exception in thread "main" java.lang.NullPointerException

and can't figure out what is going on, I know that when I run the debugger and step through the method, it crashes at the beginning of this statements

ps.setString(1, p.getCode());
ps.setString(2, p.getDescription());
ps.setDouble(3, p.getPrice());
ps.executeUpdate();

here is the results of the stack trace

at sun.jdbc.odbc.JdbcOdbcPreparedStatement.clearParameter(JdbcOdbcPreparedStatement.java:1023)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setChar(JdbcOdbcPreparedStatement.java:3057)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setString(JdbcOdbcPreparedStatement.java:766)
at DBTesterApp.insertProduct(DBTesterApp.java:165)
at DBTesterApp.main(DBTesterApp.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

解决方案

The line in the stack trace:

at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setString(JdbcOdbcPreparedStatement.java:766)

indicates that it fell over during your call to PreparedStatement.setString(), which indicates that either p.getCode() or p.getDescription() returns null.

There is therefore a problem in your Product class. Are you setting those fields correctly in the constructor?

Once you've fixed that, you may be disappointed to find that all your rows you insert have exactly the same data.

Look at you SQL to find out why:

String insertProduct = "INSERT INTO Products (ProductCode, Description, Price) VALUES ('andr', 'Beginning Android', '38.99')";

This should be:

String insertProduct = "INSERT INTO Products (ProductCode, Description, Price) VALUES (?,?,?)";

这篇关于将数据插入Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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