diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..8ddd4c7 --- /dev/null +++ b/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..2e9776e --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + em-jdbc + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.wst.jsdt.core.jsNature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/.jsdtscope b/.settings/.jsdtscope new file mode 100644 index 0000000..cca691f --- /dev/null +++ b/.settings/.jsdtscope @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.container b/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000..49c8cd4 --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.JRE_CONTAINER \ No newline at end of file diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.name b/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000..11006e2 --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Global \ No newline at end of file diff --git a/src/com/rhizome/example/dao/EmployeeDAO.java b/src/com/rhizome/example/dao/EmployeeDAO.java index ef63c49..9587551 100644 --- a/src/com/rhizome/example/dao/EmployeeDAO.java +++ b/src/com/rhizome/example/dao/EmployeeDAO.java @@ -56,10 +56,11 @@ public class EmployeeDAO { sql.append(" FROM "); sql.append(" " + TBL_NAME); try { - + this.stmt = con.prepareStatement(sql.toString()); rs = stmt.executeQuery(); while (rs.next()) { +// EmployeeDAO dao = new EmployeeDAO(con); rtnList.add(rs.getString(NM_EMPLOYEE)); } } finally { diff --git a/src/com/rhizome/example/main/Main.java b/src/com/rhizome/example/main/Main.java index 14fb220..5db3d84 100644 --- a/src/com/rhizome/example/main/Main.java +++ b/src/com/rhizome/example/main/Main.java @@ -15,6 +15,7 @@ import com.rhizome.example.util.DbUtil; */ public class Main { static Scanner sc = new Scanner(System.in); + /** * テーブル切り替え用変数 0:Employeeテーブル 1:Departmentテーブル */ @@ -69,6 +70,7 @@ public class Main { } } + /** * 社員テーブルから情報を検索する * @@ -89,11 +91,14 @@ public class Main { default: return; } + + System.out.println("取得件数" + empList.size()); + for (String emp : empList) { System.out.println(emp); } } finally { - DbUtil.closeConnection(empDao.con); + DbUtil.closeConnection(empDao.con); } } diff --git a/src/com/rhizome/example/util/DbUtil.java b/src/com/rhizome/example/util/DbUtil.java index d2bb82e..cad6649 100644 --- a/src/com/rhizome/example/util/DbUtil.java +++ b/src/com/rhizome/example/util/DbUtil.java @@ -1,13 +1,16 @@ package com.rhizome.example.util; import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; public class DbUtil { // 定数定義(共通) private static final String DB_HOST = "127.0.0.1"; private static final String DB_NAME = "rezodb"; - private static final String DB_USER = "rezouser"; - private static final String DB_PASS = "Rezo_0000"; + private static final String DB_USER = "rezouser1"; + private static final String DB_PASS = "Rezo_0001"; // 定数定義(PostgreSQL用) // private static final String DBMS = "postgresql"; @@ -41,6 +44,21 @@ public class DbUtil { * 生成されたConnectionを返却する * ※Connection生成には上記の定数を活用してください */ + //例外が2個あるためカンマでつなげることができる + public static Connection getConnection() throws SQLException { + //データベースへの接続 + con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS); + return con; + } +// try { +// Class.forName("com.mysql.cj.jdbc.Driver"); +// } catch (ClassNotFoundException e) { +// throw new SQLException("JDBCドライバが見つかりません", e); +// } +// //JDBCドライバの読み込み +// return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/rezodb" +// , "rezouser1" +// , "Rezo_0001"); /* @@ -52,8 +70,14 @@ public class DbUtil { * 例外 :SQLException * 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする */ - - + // PreparedStatement型 / DBステートメント 例外: SQLException + public static void closeStatement( PreparedStatement stmt) throws SQLException { + //引数のDBステートメントがnullでない場合 + if(stmt != null) { + //そのDBステートメントをクローズする + stmt.close(); + } + } /* * TODO 以下の仕様でメソッドを作成してください。 * @@ -63,6 +87,12 @@ public class DbUtil { * 例外 :SQLException * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する */ - + public static void closeConnection(Connection con) throws SQLException { + //引数のコネクションがnullでない場合 + if(con != null) { + //そのコネクションを切断する + con.close(); + } + } }