gpt4 book ai didi

java - 如何从 JavaDB 读取数据

转载 作者:行者123 更新时间:2023-12-01 19:09:40 24 4
gpt4 key购买 nike

我尝试创建一个简单的 Java 应用程序来从 JavaDB 读取数据。目的是从数据库中获取第一个“UNAME”变量并将其分配给“user”变量并打印它。

package giris;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
*
* @author Ibrahim
*/
public class Giris {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

String user = "";

System.out.println("hello world");
try
{
System.out.println("connecting to database");
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/deneme", "ASD", "asd");
System.out.println("succesfully connected");
PreparedStatement pr = con.prepareStatement("select UNAME from ASD.Table1");

ResultSet rs = pr.executeQuery();

while(rs.next())
{

user = rs.getString(0);
}
}
catch(Exception e)
{
System.err.println("error");
}
System.out.println(user);
}

}

This is how my DB looks like

但是,当我运行应用程序时,输出如下:

run:
hello world
connecting to database

error
BUILD SUCCESSFUL (total time: 0 seconds)

最佳答案

缺少加载驱动类逻辑,请添加

Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();

将此 derbyclient-10.2.2.0.jar 添加到类路径

希望对你有帮助

关于java - 如何从 JavaDB 读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59492655/

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