Results 1 to 4 of 4

Thread: using JDBC

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    259
    Rep Power
    10

    Default using JDBC

    I am trying to access my JDBC server at 192.168.1.7. Mysql is running on the server:
    Java Code:
    pi@raspberrypi /etc/mysql $ mysqladmin -u root -p status
    Enter password:
    Uptime: 142588  Threads: 1  Questions: 646  Slow queries: 0  Opens: 212  Flush tables: 2  Open tables: 23  Queries per second avg: 0.004
    Here is my code:
    Java Code:
    //STEP 1. Import required packages
    import java.sql.*;
    
    public class JDBC {
       // JDBC driver name and database URL
       static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
       static final String DB_URL = "jdbc:mysql://192.168.1.7/db";
    
       //  Database credentials
       static final String USER = "root";
       static final String PASS = "password";
       
       public static void main(String[] args) {
       Connection conn = null;
       Statement stmt = null;
       try{
          //STEP 2: Register JDBC driver
          Class.forName("com.mysql.jdbc.Driver");
    
          //STEP 3: Open a connection
          System.out.println("Connecting to database...");
          conn = DriverManager.getConnection(DB_URL,USER,PASS);
    
          //STEP 4: Execute a query
          System.out.println("Creating statement...");
          stmt = conn.createStatement();
          String sql;
          sql = "SELECT id, first, last, age FROM users";
          ResultSet rs = stmt.executeQuery(sql);
    
          //STEP 5: Extract data from result set
          while(rs.next()){
             //Retrieve by column name
             int id  = rs.getInt("id");
             int age = rs.getInt("age");
             String first = rs.getString("first");
             String last = rs.getString("last");
    
             //Display values
             System.out.print("ID: " + id);
             System.out.print(", Age: " + age);
             System.out.print(", First: " + first);
             System.out.println(", Last: " + last);
          }
          //STEP 6: Clean-up environment
          rs.close();
          stmt.close();
          conn.close();
       }catch(SQLException se){
          //Handle errors for JDBC
          se.printStackTrace();
       }catch(Exception e){
          //Handle errors for Class.forName
          e.printStackTrace();
       }finally{
          //finally block used to close resources
          try{
             if(stmt!=null)
                stmt.close();
          }catch(SQLException se2){
          }// nothing we can do
          try{
             if(conn!=null)
                conn.close();
          }catch(SQLException se){
             se.printStackTrace();
          }//end finally try
       }//end try
       System.out.println("Goodbye!");
    }//end main
    }//end FirstExample
    I can confirm that there is a db titled 'db'.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: using JDBC

    I am not familiar with using JDBC. However, if you want someone to help you need to provide specific information. What problems are you having?
    What error messages, if any, are being printed? And show any error messages.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    camel-man is offline Member
    Join Date
    Jan 2016
    Posts
    19
    Rep Power
    0

    Default Re: using JDBC

    Make sure that your Database driver is located on the build path.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: using JDBC

    A couple of comments.
    The MySQL driver is a type 4, so you don't need this step:
    Java Code:
          //STEP 2: Register JDBC driver
          Class.forName("com.mysql.jdbc.Driver");
    It auto registers itself.

    Assuming you are not on pre-Java 7 then you should be using try with resources:
    Java Code:
    try (Connection conn = DriverManager etc) {
        try (Statement st = conn.createStatement()) {
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
    It gets rid of the finally boiler plate code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2013, 05:26 PM
  2. Replies: 5
    Last Post: 09-25-2012, 05:36 PM
  3. JDBC Problem - com.mysql.jdbc.Driver
    By icu222much in forum Advanced Java
    Replies: 5
    Last Post: 11-22-2011, 04:54 PM
  4. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  5. Replies: 0
    Last Post: 09-28-2007, 12:56 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
  •