English 中文(简体)
将行插入 MySQL 数据库
原标题:Insert Row into MySQL database

我正与 JDTS 驱动程序使用日蚀。 我正试图在 MySQL 数据库中插入一行。 连接和语句生成得很好( 我认为) 但是当我运行我的执行更新方法时, 我得到了一个无效的指针例外 。

我有一个DB客户类 处理 DB 的连接, 然后我有一个上传者类 收集所有相关信息 并提供给我插入的方法。

我的DB客户类 :

public class DBclient {

String driver="net.sourceforge.jtds.jdbc";
String URL="xxxx";
String user="xxxx";
String pass="xxxx";
Connection connection;
Statement statement;
ResultSet rs;

public void connect() throws SQLException
{
    try {
        Class.forName(driver);
        }//try 
    catch (ClassNotFoundException e) 
        {
            e.printStackTrace();
        }//catch

    try {
        connection=DriverManager.getConnection(URL, user, pass);
        statement=connection.createStatement();

        } //try
    catch (SQLException e)
    {
        e.printStackTrace();
    }//catch
}//connect
public void insertToDB(String sqlstatement) throws SQLException
{
    int i=statement.executeUpdate(sqlstatement);
}

}//DB客户

和我的上传者课

private String testinsert="insert into xxxx (Latitude, Longitude) values (1, 2)";


public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
     setContentView(R.layout.uploader);
     initialize();
     getaddress();
     update();
     try {
        client.insertToDB(testinsert);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        Toast t=Toast.makeText(getApplicationContext(), "oops", 4);
        t.show();
    }

   }//onCreate 
public void initialize()
{
    client = new DBclient();
    geocoder = new Geocoder(this);
    Bundle coords = getIntent().getExtras();
    street = (TextView) findViewById(R.id.street);
    town = (TextView) findViewById(R.id.town);
    state = (TextView) findViewById(R.id.state);
    country = (TextView) findViewById(R.id.country);
    lat=coords.getDouble("Latitude");
    lon=coords.getDouble("Longitude");

}//initialize
public void update()
{
    street.setText(address.getAddressLine(0));
    town.setText(address.getLocality());
    state.setText(address.getAdminArea());
    country.setText(address.getCountryName());
}//update
public void getaddress()
{
    try 
    {
        addresslist = geocoder.getFromLocation(lat, lon, 1);
    } //try
    catch (IOException e) 
    {
        Toast t=Toast.makeText(getApplicationContext(), "Input Error", 2);
        t.show();
    }//catch
     address=addresslist.get(0);

}

}//上载器

从 DB client 类插入的 ToDB () 方法中丢弃 Null 指针例外 。

任何帮助都会感激不尽

最佳回答

结果发现我和用户” 有着同样的问题, https://stackoverflow.com/ questions/2247998/noclasddeffounderor-eclipse-and-and-roid'>在这里

我要做的就是把jtds. jds. jar 添加到我的资产文件夹中, 把它添加到我的建设路径中, 然后在“ 配置构建路径” 菜单中, 去订购和导出, 检查罐子文件 。

问题回答
public void insertToDB(String sqlstatement) throws SQLException
{
    int i=statement.executeUpdate(sqlstatement);
}

似乎 statement 只在 connect () 中设置了 connect () in DBClient class > 类中的 < dBClient , 但是在问题中显示的其他代码中没有使用这一方法。 因此, state 将是无效的, 其调用方法将导致 NullpoderExpendion





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签