gpt4 book ai didi

java - 如何使用 JDBC 从 mySQL 获取特定列?

转载 作者:搜寻专家 更新时间:2023-10-30 21:40:27 24 4
gpt4 key购买 nike

    PreparedStatement ps2;
ResultSet rs2;

String username = jTextField_username.getText();
String userpassword = jTextField_userpassword.getText();

String sql = "SELECT distinct kalanizin from users where username =?";
String[] resultsetarray = null;

try {

ps2 = MyConnection.getConnection().prepareStatement(sql);
ps2.setString(1, username);

rs2 = ps2.executeQuery();
String result = rs2.getString(0); // i want this String to hold the first result

大家好。我有一个问题一直困扰着我。正如标题中所说,通过使用 JDBC,我想从我的数据库中获取一列。例如

Select * from users where username = "blabla"

我希望 Java 执行此查询并获取它找到的第一行。然后放入字符串或整数。正如您在顶部看到我糟糕的代码一样,它无法做到这一点。我试图通过使用语句来完成此操作,但它仍然不起作用。谁能帮我?那将不胜感激。附言。抱歉我糟糕的变量名和使用我的本地语言作为变量

最佳答案

如果你知道列名你可以使用像getString(columnLabel)这样的方法而不是 getString(columnIndex):

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

columnLabel - the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column

关于java - 如何使用 JDBC 从 mySQL 获取特定列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50606143/

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