Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By RamyaSivakanth

Thread: Pls help me with this error

  1. #1
    june_c21 is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default Pls help me with this error

    Hi all ,

    i am new to java . when i run this code i got this error : -

    java.lang.ExceptionInInitializerError
    Caused by: java.lang.RuntimeException: Uncompilable source code - class Main is public, should be declared in a file named Main.java
    at Main$SQLInsert.<clinit>(SQLInsert.java:7)
    Exception in thread "main" Java Result: 1

    here is my code

    Java Code:
      // STEP 1: Import required packages
    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.Statement;
    import java.sql.DriverManager;
    import java.util.Scanner;
    
    public class SQLInsert {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:3306/collectionuserdetails";
    
    // Database credentials
    static final String USER = "root";
    static final String PASS = "";
    
    
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        Scanner scn = new Scanner(System.in);
        String name = null, ic = null, gender = null, address = null;
    
        try {
            // STEP 2: Register JDBC driver
            Class.forName("com.mysql.jdbc.Driver");
    
            // STEP 3: Open a connection
            System.out.print("\nConnecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            System.out.println(" SUCCESS!\n");
    
            // STEP 4: Ask for user input
            System.out.print("Enter your name: ");
            name = scn.nextLine();
    
            System.out.print("Enter IC No: ");
            ic = scn.nextLine();
    
            System.out.print("Enter your gender : ");
            gender = scn.nextLine();
            
            System.out.print("Enter your Address : ");
            address = scn.nextLine();
            
            // STEP 5: Excute query
            System.out.print("\nInserting records into table...");
            stmt = conn.createStatement();
    
            String sql = "INSERT INTO userdetails " +
                "VALUES (name, ic, gender, address)";
            stmt.executeUpdate(sql);
    
            System.out.println(" SUCCESS!\n");
    
        } catch(SQLException se) {
            se.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(stmt != null)
                    conn.close();
            } catch(SQLException se) {
            }
            try {
                if(conn != null)
                    conn.close();
            } catch(SQLException se) {
                se.printStackTrace();
            }
        }
        System.out.println("Thank you for your patronage!");
      }
    }   
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Pls help me with this error

    Is that all the code in the java file?
    Is the Java file called SQLInsert.java?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Pls help me with this error

    That is not the code that is being compiled.

    > at Main$SQLInsert.<clinit>(SQLInsert.java:7)

    In the stacktrace SQLInsert is an inner class of a class called Main. Yet in the posted code, there is no Main to speak of.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default Re: Pls help me with this error

    what i feel is Connection and Statement imports or wrong.it is in java.sql package.
    gimbal2 likes this.
    Ramya:cool:

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Pls help me with this error

    Nice catch, but probably the code will still work. They are simply the MySQL driver implementations of the interfaces in the java.sql package.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default Re: Pls help me with this error

    remove the last curly brace.
    Ramya:cool:

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 05:26 PM
  2. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 3
    Last Post: 01-23-2009, 08:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •