じゃば
他クラスのメソッド参照してる部分と、try〜catch は
main から出してみた。
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; public class Test_Select { public static void main(String[] args) { int count = 0; try { count = ConnectSql(); } catch (SQLException sql) { System.out.println("sql : " + sql.toString()); } // データが1件もなかったとき if (count == 0) { System.out.println("登録されているIDを入力してください。"); } } // 入力とSQL処理 protected static int ConnectSql() throws SQLException { Db DbClass = new Db(); ResultSet rs = null; Connection connect = null; int i = 0; try { connect = DbClass.DbConnection(); System.out.println("検索したい 商品区分IDを入力"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); String sql = SelectSql(str); rs = DbClass.SelectResult(connect, sql); while (rs.next()) { if (i == 0) { System.out.println("***商品名***"); } System.out.println(rs.getString("NAME")); i++; } } catch (IOException io) { System.out.println("io : " + io.toString()); } catch (Exception ex) { System.out.println("ex : " + ex.toString()); } finally { connect.close(); } return i; } // 渡された id をもとに select 文作成 protected static String SelectSql(String id) { StringBuilder sql = new StringBuilder(); sql.append("select "); sql.append("ITEM_NAME from "); sql.append("ITEM, DIVISION_MST "); sql.append("where "); sql.append("DIVISION_MST.DIV_ID = ITEM.ITEM_DIV and "); sql.append("DIVISION_MST.DIV_ID = '"); sql.append(id); sql.append("'"); return sql.toString(); } }