无法编写SQLite数据库Java

时间:2014-02-22 19:36:53

标签: java xml sqlite

我正在尝试使用JDBC库在java中创建SQLite数据库。我的代码创建数据库很好,但是当涉及在数据库中创建表时,它实际上并不创建表。我知道这是因为我试图打印记录集,但它没有到达打印语句。我的总体目标是解析XML,然后将其写入相应的字段,以便在我的Android应用程序中使用。这是我的代码。

import java.io.File;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

import java.sql.*;

public class TestingXML
{

public static void main(String[] args)
{

    try
    {

        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection("jdbc:sqlite:productrecalls.db");
        Statement stat = conn.createStatement();
        stat.executeUpdate("drop table if exists products;");
        stat.executeUpdate("create table products (date, brandname, productdescription, reason, company, link);");
        PreparedStatement prep = conn.prepareStatement("insert into products values (?, ?, ?, ?, ?, ?);");

        File fXmlFile = new File("/Users/Trevor/Desktop/1.7-RecallsDataSet.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("PRODUCT");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                String day = eElement.getElementsByTagName("DATE").item(0).getTextContent().substring(5, 7);
                String month = eElement.getElementsByTagName("DATE").item(0).getTextContent().substring(8, 11);
                String year = eElement.getElementsByTagName("DATE").item(0).getTextContent().substring(12, 17);

                String date = month + "-" + day + "-" + year;
                String brandname = eElement.getElementsByTagName("BRAND_NAME").item(0).getTextContent();
                String productdescription = eElement.getElementsByTagName("PRODUCT_DESCRIPTION").item(0).getTextContent();
                String reason = eElement.getElementsByTagName("REASON").item(0).getTextContent();
                String company = eElement.getElementsByTagName("COMPANY").item(0).getTextContent();
                String link = eElement.getElementsByTagName("COMPANY_RELEASE_LINK").item(0).getTextContent();

                prep.setString(1, date);
                prep.setString(2, brandname);
                prep.setString(3, productdescription);
                prep.setString(4, reason);
                prep.setString(5, company);
                prep.setString(6, link);

                conn.setAutoCommit(false);
                prep.executeBatch();
                conn.setAutoCommit(true);

            }
        }
        ResultSet rs = stat.executeQuery("select * from products;");
        while (rs.next())
        {
            System.out.println(" ");
            System.out.println("date = " + rs.getString("date"));
            System.out.println("brandname = " + rs.getString("brandname"));
            System.out.println("productdescription = " + rs.getString("productdescription"));
            System.out.println("reason = " + rs.getString("reason"));
            System.out.println("company = " + rs.getString("company"));
            System.out.println("link = " + rs.getString("link"));
        }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

    System.out.println("Done.");

}
}

1 个答案:

答案 0 :(得分:1)

你很接近:)“create table”语句的语法需要更多信息。这是SQLite文档页面: enter link description here

这是我最近在项目中使用的示例语句。它为您提供了create table语句的具体示例:

create table authors (id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, first_name varchar(25) NOT NULL,
last_name varchar(25) NOT NULL,created_at datetime, updated_at datetime);

请注意!您需要指定列数据类型。我还向您展示了如何创建主键(其值由数据库生成)。

希望有帮助:)