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 this is the part I'm mistaken
value = val;
}catch(SQLException e){
JOptionPane.showMessageDialog(null, e);
}
}
public static String getValue(){
return value;
}
}
そして、私のデータベースのテーブルは次のようになっています。
"table: mytable, schema: user"
column1 column2
-------- --------
john abc
jeff xyz
ian 123
したがって、基本的に、JOptionPaneは文字列出力として「john」を表示する必要があります。それが私がやりたいことです。助けてください。本当にこれが必要です。
- 私は自分のデータベースが接続されていることを100%確信しています。それは、どういうわけか間違った方法を実行しているために、必要な値を取得できないということです。