Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java程序连接JSON数据库(记事本和命令提示符)_Java_Json_Jdbc - Fatal编程技术网

用java程序连接JSON数据库(记事本和命令提示符)

用java程序连接JSON数据库(记事本和命令提示符),java,json,jdbc,Java,Json,Jdbc,所以我一直在搜索将我制作的JSON数据库连接到java程序,我似乎找不到任何适合这个特定情况的东西,所以我尝试使用JDBC教程(),尽管我遇到了一些问题 我所拥有的- 我的数据库 一个用于设置连接的示例程序(我已经尝试用它来实现json,这里有一些我最大的问题) json LIB(我相信我已经成功地将它们添加到了我的类路径中,尽管我已经设法找到了如何做到这一点) 我使用的是什么版本/类似版本- 带有记事本和命令提示符的java(j2sdk1.4.2) json-lib-2.4-jdk13(总共3

所以我一直在搜索将我制作的JSON数据库连接到java程序,我似乎找不到任何适合这个特定情况的东西,所以我尝试使用JDBC教程(),尽管我遇到了一些问题

我所拥有的- 我的数据库 一个用于设置连接的示例程序(我已经尝试用它来实现json,这里有一些我最大的问题) json LIB(我相信我已经成功地将它们添加到了我的类路径中,尽管我已经设法找到了如何做到这一点)

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

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

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

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个元素)

出于测试目的,我只想获取元素数组中第一个对象的属性(即“Hydrogen”)

问题- 对于静态最终字符串DB_URL=“”;JSON使用什么格式


我相信我仍然需要导入JSON的东西,但我还没有找到如何导入的方法(如上面所述,我得到的最多的结果是将我下载的3个JAR添加到我的类路径中)。这就引出了我的问题,我如何导入我下载并添加到我的类路径中的库?

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

为什么要使用Java 1.4.2?这是一个古老的版本,考虑到自上次发布以来已修复的安全漏洞,这也是一个风险。您使用的是哪个JSON JDBC驱动程序(从外观上看,
JSON-lib-2.4-jdk13
不是JDBC驱动程序)。对于驱动程序的正确JDBC url,您需要检查该驱动程序的文档。我在高中学习计算机科学120课程,这门课程让我使用java 1.4.2,这个数据库内容是用于一个额外的项目(元素周期表程序,完成后可从学校网站下载)我还没有太多的编程经验,所以我决定用教我的版本来做这个程序。关于使用特定的json库,下面是(本页最终引导我下载了3个json jar文件):json-lib-x.x-jdk13与JDK 1.3.1及以上兼容。该库不是JDBC驱动程序,而是json解析器。您是从哪里想到它可以用作JDBC驱动程序的?关于Java 1.4.2的使用,告诉你的老师,他至少已经过时12年了,让你、你的同学和学校面临安全风险。我想这是从混乱中得到的。现在我知道我需要找到一个JDBCJSON驱动程序。但是,我使用的是JSON,而教程没有给出JSON的示例。我会尝试在教程给我的内容中实现json,但是教程后面的一些部分显示了一个图表。图表显示不同的数据库使用不同的URL格式。(图表中的硬连接部分是JDBC-Connections)。例如,根据教程,oracle数据库的格式为jdbc:oracle:thin:@hostname:port Number:databaseName。与msql格式(尤其是格式右侧)相比,它有不同之处。实际上,对于此类应用程序,使用Volley库要容易得多,它内置了读取JSON响应的方法-这里有一个链接,指向一个我在学习Volley()时发现很有用的教程,希望这是编辑帮助,我试着转到一个新的行-了解到按enter键“添加注释”对你来说绝对有必要使用JSON来解析数据,如果你遵循教程,你链接的数据可以很好地读取,而不需要JSON-刚刚测试了它..嗯,我必须再看一次。我将失去互联网大约一个小时(学校结束)
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!");
}
}
{
"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"
        }
    ]       
    },
}