ローカル環境(Ubuntu 20.04)で簡素なCUIのJavaプログラムを作成しています。
目的は、VirtualBox上のゲストOS(Ubuntu Server 20.04)のMySQLデータベースにアクセスし、
SELECT文で対象のテーブルからデータを抽出することです。
サーバとDBへのアクセスは出来ている事確認済みなのですが、
プログラムを動かすと、下記エラーが表示されます。
※DB名、テーブル名は確認済みです。
java.sql.SQLSyntaxErrorException: Table 'budgettracker.data2020' doesn't exist
私の方でも検索はしてみたのですが、参考になる資料が見当たらず、ご助力頂けますと幸いです。
よろしくお願いいたします。
BudgetTrackerDto.java
package com.jdbc.budgettracker.core; import java.sql.Date; public class BudgetTrackerDto { private int id; private Date date; private String storeName; private String productName; private String productType; private int price; public BudgetTrackerDto() { } public BudgetTrackerDto(int id, Date date, String storeName, String productName, String productType, int price) { super(); this.id = id; this.date = date; this.storeName = storeName; this.productName = productName; this.productType = productType; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getStoreName() { return storeName; } public void setStoreName(String storeName) { this.storeName = storeName; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getProductType() { return productType; } public void setProductType(String productType) { this.productType = productType; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } }
BudgetTrackerDao.java
package com.jdbc.budgettracker.dao; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Properties; import com.jdbc.budgettracker.core.BudgetTrackerDto; public class BudgetTrackerDao { BudgetTrackerDto btd; private Connection myConn; private static final String SQL = "select * from data2020;"; public List<BudgetTrackerDto> selectAll() throws FileNotFoundException, IOException { List<BudgetTrackerDto> budgetList = new ArrayList<>(); // get db properties Properties props = new Properties(); props.load(new FileInputStream("/home/yosuke/BudgertTrackerGui/BudgetTrackerAppGui/sql/config.properties")); String user = props.getProperty("user"); String password = props.getProperty("password"); String dburl = props.getProperty("dburl"); try (Connection conn = DriverManager.getConnection(dburl, user, password); PreparedStatement ps = conn.prepareStatement(SQL)) { try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { btd = new BudgetTrackerDto(); btd.setDate(rs.getDate("Date")); btd.setStoreName(rs.getString("StoreName")); btd.setProductName(rs.getString("ProductName")); btd.setProductType(rs.getString("Type")); btd.setPrice(rs.getInt("Price")); budgetList.add(btd); } } } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { } return budgetList; } }
BudgetTrackerMain.java
package com.jdbc.budgettracker.main; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import com.jdbc.budgettracker.dao.BudgetTrackerDao; import com.jdbc.budgettracker.core.BudgetTrackerDto; import java.util.Scanner; public class BudgetTrackerMain { public static void main(String[] args) throws FileNotFoundException, IOException { // TODO Auto-generated method stub BudgetTrackerDao budgetTrackerDao; Map<Integer, String> initialSwitchMap = new HashMap<>(); initialSwitchMap.put(1, "Select"); initialSwitchMap.put(2, "Insert"); initialSwitchMap.put(3, "Update"); initialSwitchMap.put(4, "Delete"); Scanner initialSwitchScanner = new Scanner(System.in); int initialNumInt = 0; do { // String[] initialList = new String[] {initialSwitchMap.get(1), // initialSwitchMap.get(2), initialSwitchMap.get(3), initialSwitchMap.get(4)}; for (Map.Entry<Integer, String> list : initialSwitchMap.entrySet()) { System.out.println(list.getKey() + ":" + list.getValue()); } System.out.print("Select a number: "); String initialNumStr = initialSwitchScanner.next(); initialNumInt = Integer.parseInt(initialNumStr); } while (initialNumInt >= 5 || initialNumInt <= 0); switch (initialNumInt) { case 1: // Select System.out.println("You chose " + initialSwitchMap.get(1)); budgetTrackerDao = new BudgetTrackerDao(); Map<Integer, String> selectSwitchMap = new HashMap<>(); selectSwitchMap.put(1, "Select All"); selectSwitchMap.put(2, "Select by Date"); selectSwitchMap.put(3, "Select by Store"); selectSwitchMap.put(4, "Select by Product"); selectSwitchMap.put(5, "Select by Type"); selectSwitchMap.put(6, "Select by Price"); Scanner selectScanner = new Scanner(System.in); int selectScannerNumInt = 0; do { // String[] selectScanList = new String[] {selectSwitchMap.get(1), // selectSwitchMap.get(2), selectSwitchMap.get(3), selectSwitchMap.get(4), // selectSwitchMap.get(5)}; for (Map.Entry<Integer, String> selectScanlist : selectSwitchMap.entrySet()) { System.out.println(selectScanlist.getKey() + ":" + selectScanlist.getValue()); } System.out.print("Select a number: "); String selectScannerNumStr = selectScanner.next(); selectScannerNumInt = Integer.parseInt(selectScannerNumStr); } while (selectScannerNumInt > 7 || selectScannerNumInt <= 0); do { if (selectScannerNumInt == 1) { System.out.println("Select All----------"); List<BudgetTrackerDto> btd = budgetTrackerDao.selectAll(); for (BudgetTrackerDto b : btd) { System.out.println(b.getDate() + ", " + b.getStoreName() + ", " + b.getProductName() + ", " + b.getProductType() + ", " + b.getPrice()); } } } while (selectScannerNumInt > 7 || selectScannerNumInt <= 0); break; } } }
config.properties
user=user_name password=password dburl=jdbc:mysql://192.168.0.44:3306/budgettracker?allowPublicKeyRetrieval=true&useSSL=false
select * from information_schema.tables where table_schema='budgettracker' and table_name='data2020';
回答1件
あなたの回答
tips
プレビュー