Note that there are some explanatory texts on larger screens.

plurals
  1. POJDBC query + Scanner.next() gives InputMismatchException
    primarykey
    data
    text
    <p>I have written the simplest implementation of DBMS using Java's JDBC. In my app I gve users ability to perform CRUD operations on some simple mysql database. Everything done in console. Problem is that when users chooses operation from menu (queries currently hardcoded) and then provides query a <code>java.util.InputMismatchException</code> exception is thrown. Any ideas why this might be happening ? Here's the code:</p> <pre><code>import java.sql.*; import java.util.Scanner; public class Main { public static void main(String[] args) { Base base = new Base(); boolean result = false; try{ base.connect(); }catch(SQLException se){ se.printStackTrace(); }catch(Exception e){ e.printStackTrace(); } Scanner sc = new Scanner(System.in); int menuChoice = -1; String query = ""; while(menuChoice != 0){ showMenu(); menuChoice = sc.nextInt(); System.out.println("Please provide your query : "); switch(menuChoice){ case 1: query = sc.next(); result = base.insert(query); break; case 2: query = sc.next(); result = base.update(query); break; case 3: query = sc.next(); result = base.retrieve(query); break; case 4: query = sc.next(); result = base.delete(query); break; case 0: System.out.println("Bye bye"); base.connection = null; System.exit(0); } } } public static void showMenu(){ System.out.println("Welcome to simple JDBC example application./n"); System.out.println("Choose desired operation:\n\n"); System.out.println("1. Insert new instance"); System.out.println("2. Update existing instance"); System.out.println("3. Lookup"); System.out.println("4. Delete instance"); System.out.println("0. Exit"); System.out.print("\n\n Select: "); } } class Base { private String username = ""; private String password = ""; private String dbname = ""; private String servername = ""; private Statement stmt = null; Connection connection = null; public Base(){ } public boolean create(){ return true; } public void connect() throws Exception{ String driverName = "com.mysql.jdbc.Driver"; Class.forName(driverName); String url = "jdbc:mysql://" + servername + "/" + dbname; connection = DriverManager.getConnection(url, username, password); stmt = connection.createStatement(); } public boolean insert(String statement){ try{ int i=stmt.executeUpdate(statement); System.out.println("Successfully inserted."); return true; }catch(SQLException se){ System.out.println("Inserting data failed."); return false; } } public boolean update(String statement){ try{ int i=stmt.executeUpdate(statement); System.out.println("Successfully updated."); return true; }catch(SQLException se){ System.out.println("Updating data failed."); return false; } } public boolean retrieve(String query){ try{ ResultSet rs = stmt.executeQuery(query); System.out.println("Successfully retrieved :"); while (rs.next()){ System.out.println(rs.getRow()+". "+rs.toString()); } return true; }catch(SQLException se){ System.out.println("Updating data failed."); return false; } } public boolean delete(String statement){ try{ int i=stmt.executeUpdate(statement); System.out.println("Successfully deleted."); return true; }catch(SQLException se){ System.out.println("Deleting data failed."); return false; } } } /* CREATE TABLE users ( user_login varchar(10) PRIMARY KEY NOT NULL, user_password varchar(20) NOT NULL ); CREATE TABLE groups ( group_id varchar(10) PRIMARY KEY NOT NULL, group_name varchar(50), group_description varchar(200) ); CREATE TABLE groups_users ( user_login varchar(10), group_id varchar(10), FOREIGN KEY (user_login) REFERENCES users(user_login), FOREIGN KEY (group_id) REFERENCES groups(group_id)); */ </code></pre> <hr> <p>EDIT: Traceback</p> <pre><code>Select: 1 Please provide your query : SELECT * FROM users Inserting data failed. Welcome to simple JDBC example application./n Choose desired operation: Exception in thread "main" java.util.InputMismatchException 1. Insert new instance at java.util.Scanner.throwFor(Scanner.java:840) 2. Update existing instance at java.util.Scanner.next(Scanner.java:1461) 3. Lookup at java.util.Scanner.nextInt(Scanner.java:2091) 4. Delete instance 0. Exit at java.util.Scanner.nextInt(Scanner.java:2050) at task.Main.main(Main.java:26) Select: Java Result: 1 </code></pre> <p>so error comes from line <code>menuChoice = sc.nextInt();</code>. What more, when I've added another scanner instance just for queries, picking operation type returns user back to the menu without asking for query.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload