繁体   English   中英

SQL 查询到 Java 字符串

[英]SQL query to Java String

我有一个简单的问题,但我找不到正确的方法。 When we use a query from SQL via Java we usually print the results in a JTable, my doubt it's ¿Is there a way to get that query into a Java String?

示例:我做了一个查询statement.executeQuery("SELECT Tiempo_elaboracion FROM catalogo_arreglos WHERE Id_arreglo='"+id_arre+"'");

结果是 00:07:00 ¿如何将该结果保存到 Java 字符串中?

结果实际上是一个ResultSet ,它包含所有匹配的行(可能只有一个)和所有选定的列(可能只有一个)。

一些序列,如

 if (!rs.first()) { ... error no first row ... }
 String str = rs.getString("Tiempo_elaboracion");

应该这样做。 但是不要把这当成福音,我只是偷看了文档 以它为起点。

我找到了一个额外的解决方案来补充,我为所有将来需要帮助的人发帖。

ResultSet myRS;
PreparedStatement myQuery
//Under this line "con" is the name of my connection//
myQuery = con.prepareStatement(SELECT value FROM table WHERE condition)
myRS = myQuery.executeQuery();
//The purpose is the save the value of the query into the String called "time"
if(myRS.next()){
String time = myRS.getString(1);
//The next line only print the String "time" for corroborate the procces//
System.out.println(""+time);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM