0

My JDBC code isn't doing anything at all. It'll print out the "connecting to database" statement but that's it. I've referenced the JDBC jar file so i'm a little puzzled why my code isn't working. The code i've been trying to get working is below.

package exampleDatabase;


import java.sql.*; 


public class Main {

        // TODO Auto-generated method stub


        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
           static final String DB_URL = "jdbc:mysql://localhost:81/companylist";

           //  Database credentials
           static final String USER = "root";
           static final String PASS = "";

           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, firstName, lastName,Email FROM Staff";
              ResultSet rs = stmt.executeQuery(sql);

              //STEP 5: Extract data from result set
              while(rs.next()){
                 //Retrieve by column name
                 int ID  = rs.getInt("ID");
                 String firstName = rs.getString("firstName");
                 String lastName = rs.getString("lastName");
                 String Email = rs.getString("Email");

                 //Display values
                 System.out.print("ID: " + ID);
                 System.out.print(", First: " + firstName);
                 System.out.println(", Last: " + lastName);
                 System.out.println(", Last: " + Email);

              }
              //STEP 6: Clean-up environment
              rs.close();
              stmt.close();
              conn.close();
           }catch(SQLException se){
              //Handle errors for JDBC
              se.printStackTrace();
           } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           }
    }
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • are you sure port 81 is the right port? https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html – maxhuang Apr 25 '17 at 02:01
  • Did you get error message? and is the database port address correct? are you sure it is `81`? – Kh.Taheri Apr 25 '17 at 02:02
  • Hi I think it is a port issue. Please check this post and see if it resolves your issue. http://stackoverflow.com/a/26788332/4022947 Thanks – Aritra Chatterjee Apr 25 '17 at 02:03
  • I'm an utter idiot. Thankyou so much that has fixed it. –  Apr 25 '17 at 02:05
  • Database port address is not the web server address; so it is not related to WAMP .. just change it to `3306`, which is the default pot address for MySQL. – Kh.Taheri Apr 25 '17 at 02:05

2 Answers2

0

The database server's Port Address is not the web server's port. So, to solve this you should change the connection URL to jdbc:mysql://localhost:3306/companylist, as 3306 is the default port address of MySQL database server.

Kh.Taheri
  • 946
  • 1
  • 10
  • 25
-1

jdbc:mysql://localhost:81/companylist is not valid.

Since you are using mysql database the default port number of server is 3306.

So try this, jdbc:mysql://localhost:3306/companylist

it will run....

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
Daneshkhan
  • 41
  • 4