gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:48:35 29 4
gpt4 key购买 nike

所以我一直在寻找将我创建的 JSON 数据库连接到 Java 程序的方法,我似乎找不到适合这种特定情况的任何内容,因此我尝试使用 JDBC 教程(http://www.tutorialspoint.com/jdbc/index.htm ),尽管我遇到了一些问题。

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

我使用的是什么版本/类似版本 -带有记事本和命令提示符的 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 - 示例程序部分下。):

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 = "";我应该为 JSON 使用什么格式?

我相信我仍然需要导入 JSON 的东西,但我无法找到如何(如顶部所述,我已经得到的最远的是将我下载的 3 个 jar 添加到我的类路径).所以这引出了我的问题,我如何导入我下载并添加到我的类路径中的库?

最佳答案

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

关于java - java程序连接JSON数据库(记事本+命令提示符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35755854/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com