我正在使用连接到 DB2 数据库的 java 应用程序。我想要做的是从我查询的 DB2 表中获取一个值并将其分配给 java 中的字符串值,你能教我怎么做吗?我自己尝试过,我会向您展示我的代码,但我认为并知道这是错误的,请帮助...
public class GetValueFromDB2 implements ActionListener{
static String value;
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source==testValue){ //testValue = a button to test my results
setValue(getValue());
JOptionPane.showMessageDialog(null, value);
}
}
public static void main(String[] args){
//GUI Implementations here...
}
public static void setValue(String val){
try{
Connection con = DriverManager.getConnection("jdbc:db2://localhost:50000/db","username","password");
String sql = "select column1 from \"user\".\"mytable\" where column2='abc'";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
val = rs.getString(1); //I think …Run Code Online (Sandbox Code Playgroud)