Results 1 to 4 of 4

Thread: [RESOLVED] cant get java sql code to work, what does System.exit(); do?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Resolved [RESOLVED] cant get java sql code to work, what does System.exit(); do?

    Hello

    I cant get this piece of java code to compile

    [code]
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.*;

    public class JDBCDemo {
    Connection db_connection;
    Statement db_statement;
    ResultSet result;

    public void loadDriver(){

    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    System.out.println ("+++++++++++++++++++");
    System.out.println ("+ database driver loaded +");
    System.out.println ("+++++++++++++++++++");
    } catch (Exception excep) {
    System.out.println ("Unable to load database driver: n" + excep);
    System.exit(0);
    }
    }

    public void connectToDB(){

    String dbName = "Business";
    String url = "jdbcdbc:" + dbName;

    try {
    db_connection = DriverManager.getConnection (url, "dba", "sql");
    System.out.println ("+++++++++++++++++++++++++++++++");
    System.out.println ("+ connected to database to DB + " + dbName);
    System.out.println ("+++++++++++++++++++++++++++++++");

    } catch (Exception excep) {
    System.out.println ("Unable to connect to database: n" + excep);
    System.exit(0);
    }
    }

    public void createStatement(){

    try {
    db_statement = db_connection.createStatement();
    System.out.println ("+++++++++++++++++++++");
    System.out.println ("+ statement created +");
    System.out.println ("+++++++++++++++++++++");
    } catch (Exception excep) {
    System.out.println ("Unable to create statement: n" + excep);
    System.exit(0);
    }
    }

    public void createTable(){

    String custTable =
    "CREATE TABLE tblCustomers (CustomerID INTEGER NOT NULL,[Name] TEXT(50) NOT NULL)";
    try {
    db_statement.executeUpdate(custTable);
    System.out.println ("+++++++++++++++++");
    System.out.println ("+ table created +");
    System.out.println ("+++++++++++++++++");
    } catch (Exception excep) {
    System.out.println ("Unable to create table: n" + excep);
    System.exit();

    }
    }


    }
    [/code}

    when i try to compile it, it comes up with the following error

    C:\JDBCApp>javac jdbcapp.java
    .\JDBCDemo.java:66: exit(int) in java.lang.System cannot be applied to ()
    System.exit();
    ^
    1 error

    if i actually comment out the following line of code, it works ok
    Code:
       System.exit();
    why am i getting the error, is the System.exit() essential?
    Last edited by vb_student; Dec 2nd, 2006 at 07:52 AM.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: cant get java sql code to work, what does System.exit(); do?

    dound out the mistake

    it should have been system.exit(0);

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: [RESOLVED] cant get java sql code to work, what does System.exit(); do?

    Actually, it should be System.exit(1) in all these cases: a program exit code of 0 indicates success, which clearly is not the case.

    A better error handling strategy would be to let the exceptions propagate and only catch them somewhere near main().
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: [RESOLVED] cant get java sql code to work, what does System.exit(); do?

    thanks for the reply and the insight into error handling

    i am pretty much a noob, you know any good online resources for error handling

Posting Permissions

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



Click Here to Expand Forum to Full Width