将JSON数据库与java程序连接(记事本+命令提示符)

时间:2016-03-02 18:59:26

标签: java json jdbc

所以我一直在寻找将我制作的JSON数据库连接到java程序,我似乎无法找到适用于这个特定情况的任何内容,所以我尝试使用JDBC教程(http://www.tutorialspoint.com/jdbc/index.htm),虽然我遇到了一些问题。

我有什么 - 我的数据库 一个用于建立连接的示例程序(我试图用这个实现json,这里有一些我最大的问题) json libs(我相信我已成功将这些添加到我的类路径中,尽管我已经设法找到了怎么做)

我使用的是什么样的/类似 - java(j2sdk1.4.2)带有记事本和命令提示符 json-lib-2.4-jdk13(总共3个jar文件 - 从https://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/下载)

这是我的示例程序(未更改的示例代码可以在上面的tutorialspoint.com链接中找到,位于页面左侧的JDBC - Sample Program部分。):

import java.sql.*;

public class DatabaseTest
{

static final String JGBC_DRIVER = "com.json.jdbc.Driver"; 
    //Tutorial had the following: com.mysql.jdbc.Driver
static final String DB_URL = "";
   //Tutorial states: jdbc:mysql://localhost/EMP, though I saw something that was 

//基本上说不同的数据库类型有不同的格式......

static final String USER = "test";
static final String PASS = "password";

public static void main(String[]args) 
{
    Connection conn = null;
    Statement stmt = null;

    try
    {
        Class.forName("com.json.jdbc.Driver");

        System.out.println("Connecting to database");
        conn = DriverManager.getConnection(DB_URL,USER,PASS);

        System.out.println("Creating statement...");
        stmt = conn.createStatement();
        /*String sql;  This section is commented so I could javac 
        *(clearing out whatever errors I could
        * tutorial then states the following, however I believe this 
        *is specific to a database format
        *sql = "SELECT id, first, last age FROM Employees";
        *The following line is how I think it would work with JSON...
        *
        *sql = Elements[0];
        *
        *ResultSet rs = stmt.executeQuery(sql);
        *
        *while(rs.next())
        *{
        *   String strName = rs.getString(name);
        *   System.out.println("Element name: " + strName);
        *}
        *
        rs.close();
        */
        stmt.close();
        conn.close();
    }catch(SQLException se){
        se.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try
        {
            if(stmt != null)
                stmt.close();
        }catch(SQLException se2){
        }
        try
        {
            if(conn != null)
                conn.close();
        }catch(SQLException se){
            se.printStackTrace();
        }
    }
    System.out.println("Goodbye!");
}
}

这是我的数据库的一部分(它包含元素周期表的信息,通过所有118个元素)。

出于测试目的,我只想获取Elements数组中第一个对象的属性(这只是" Hydrogen")

{
"Elements" : [
    {
    "name" : "Hydrogen",
    "Symbol" : "H",
    "atomicNumber" : "1",
    "electronegativity" : "2.2",
    "group" : "Hydrogen",
    "ionCharge1" : "1+",
    "ionCharge2" : "1-",
    "molarMass" : "1.01",
    "naturalState" : "Gas",
    "synthetic" : "false",
    "diatomic" : "true",
    "columnNumber" : "1",
    "columnCode" : "IA",
    "row" : "1",

    "nobleGasConfiguration" : [
        {
        "term:" : "No Noble Gas Configuration",
        "superScript" : "-"
        }
    ],
    "electronConfiguration" : [
        {
        "term" : "1s",
        "superScript" : "1"
        }
    ]
    },
    {
    "name" : "Helium",
    "Symbol" : "He",
    "atomicNumber" : "2",
    "electronegativity" : "-",
    "group" : "NobleGas",
    "ionCharge1" : "-",
    "ionCharge2" : "-",
    "molarMass" : "4.00",
    "naturalState" : "Gas",
    "synthetic" : "false",
    "diatomic" : "false",
    "columnNumber" : "18",
    "columnCode" : "VIIIA",
    "row" : "1",

    "nobleGasConfiguration" : [
        {
        "term" : "[He]",
        "superScript" : "-"
        }
    ],      
    "electronConfiguration" : [
        {
        "term" : "1s",
        "superScript" : "2"
        }
    ]       
    },
    {
    "name" : "Lithium",
    "Symbol" : "Li",
    "atomicNumber" : "3",
    "electronegativity" : "1.0",
    "group" : "AlkaliMetal",
    "ionCharge1" : "1+",
    "ionCharge2" : "-",
    "molarMass" : "6.94",
    "naturalState" : "Solid",
    "synthetic" : "false",
    "diatomic" : "false",
    "columnNumber" : "1",
    "columnCode" : "IA",
    "row": "2",

    "nobleGasConfiguration" : [
        {
        "term" : "[He]",
        "superScript" : "-"
        },
        {           
        "term" : "2s",
        "superScript" : "1"
        }
    ],
    "electronConfiguration" : [
        {
        "term" : "1s",
        "superScript" : "2"
        },
        {
        "term" : "2s",
        "superScript" : "1"
        }
    ]       
    },
}

问题 - 对于静态最终字符串DB_URL ="&#34 ;;我将使用什么格式的JSON?

我相信我仍然需要导入JSON的东西,但是我无法知道如何(如上所述,我已经得到的最远的东西已经添加了3个罐子我已经' ve下载到我的classpath)。这导致我的问题,我如何导入我已下载并添加到我的类路径的库?

1 个答案:

答案 0 :(得分:1)

您的DB_URL应该是数据库的路径 - 如果没有数据库的路径,Java应用程序将不知道在何处查询数据库。再次使用教程中的示例(jdbc:mysql://localhost/STUDENTS