Results 1 to 2 of 2
  1. #1
    jellora is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default how to get input from console using scanner and store it in database(eg:msaccess)

    i did a program to directly assignin values and storing to the dattabase n sql queries too...
    now i want to noe
    1)how to get the input frm console and store it in database
    2)how to read the data from a file and then store in a database
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication6;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    /**
     *
     * @author Admin
     */
    public class JavaApplication6 {
    
        /**
         * @param args the command line arguments
         */
         private static final String USERNAME = "admin";
    private static final String PASSWORD = "welcome";
    private static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
     
    private static final String URL ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=E:\\stud.mdb;}";
     
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
          
        
           
    String nam="janet",addre="hennur",m1="95",m2="96";
    int tot=Integer.parseInt(m1)+Integer.parseInt(m2);
    Connection connection = null;
    try {
    Class.forName(DRIVER);
    connection = DriverManager.getConnection(URL);
    Statement stm=connection.createStatement();
    stm.executeUpdate("Insert into information(name,address,m1,m2,total)values('"+nam+"','"+addre+"',"+m1+","+m2+","+tot+")");
    
    } catch (SQLException e) {
    } finally {
    connection.close();
    }
    }
        
    }

  2. #2
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: how to get input from console using scanner and store it in database(eg:msaccess)

    Quote Originally Posted by jellora View Post
    i did a program to directly assignin values and storing to the dattabase n sql queries too...
    now i want to noe
    1)how to get the input frm console and store it in database
    2)how to read the data from a file and then store in a database
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication6;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    /**
     *
     * @author Admin
     */
    public class JavaApplication6 {
    
        /**
         * @param args the command line arguments
         */
         private static final String USERNAME = "admin";
    private static final String PASSWORD = "welcome";
    private static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
     
    private static final String URL ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=E:\\stud.mdb;}";
     
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
          
        
           
    String nam="janet",addre="hennur",m1="95",m2="96";
    int tot=Integer.parseInt(m1)+Integer.parseInt(m2);
    Connection connection = null;
    try {
    Class.forName(DRIVER);
    connection = DriverManager.getConnection(URL);
    Statement stm=connection.createStatement();
    stm.executeUpdate("Insert into information(name,address,m1,m2,total)values('"+nam+"','"+addre+"',"+m1+","+m2+","+tot+")");
    
    } catch (SQLException e) {
    } finally {
    connection.close();
    }
    }
        
    }

    For reading input from console you can use java.util.Scanner class as:

    java.util.Scanner scanner = new Scanner(System.in);
    String str = scanner.next();

    For storing it in database use the JDBC-ODBC driver to connect and use java.sql package...
    create the connection and Statement to execute query.

    Best of luck...

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2011, 02:03 PM
  2. Using Scanner console for Graphics
    By bensewards in forum New To Java
    Replies: 3
    Last Post: 03-17-2010, 03:16 AM
  3. Simple DATABASE FROM MSACCESS But ERROR!!!!!!!!
    By Elshan0011 in forum New To Java
    Replies: 2
    Last Post: 12-15-2009, 07:46 PM
  4. Replies: 2
    Last Post: 10-11-2009, 01:31 PM
  5. Read from console (Scanner Class)
    By hey in forum New To Java
    Replies: 10
    Last Post: 12-11-2007, 11:11 PM

Posting Permissions

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