0

I'm reading through the JDBC API Tutorial and Reference 3/E (MAN, what a dry tome), and I'm having trouble connecting my Java program to my MySQL database using the MySQL Connector supplied by Oracle.

I've placed it in my folder

C:\Windows\Sun\Java\mysql-connector-java-5.1.24

and I've pointed my Workspace CLASSPATH in JGrasp to

C:\Windows\Sun\Java\mysql-connector-java-5.1.24\mysql-connector-java-5.1.24-bin.jar

I'm trying to connect to a database called "Coffee." It definitely exists:

mysql> show databases;
+---------------------+
| Database            |
+---------------------+
| information_schema  |
| coffee              |
| mysql               |
| performance_schema  |
| phpmyadmin          |
+---------------------+
5 rows in set (0.03 sec)

here's my Java code. (If you have the book, I'm on page 88. The only difference between my code and theirs is some comments, and that I'm going with MySQL.)

//first, import sql package
import java.sql.*;

//name class CreateCoffees
public class CreateCoffees { 

    public static void main(String[] args){

        String url = "jdbc:mysql://127.0.0.1:coffee";

        //declare variables 
        Connection conn;

        String createString =   "create table COFFEES " +
                                        "(COF_NAME varchar(32), " +
                                        "SUP_ID int, " +
                                        "PRICE float, " +
                                        "SALES int, " +
                                        "TOTAL int)";

        Statement stmt;


        //instructions
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch(java.lang.ClassNotFoundException cnfe){
            System.out.println("Class Not Found - " + cnfe.getMessage());
        }       

        try{
            conn = DriverManager.getConnection(url, "root", "");

            stmt = conn.createStatement();

            stmt.executeUpdate(createString);

            stmt.close();
            conn.close();
        }
        catch(SQLException sqle){
            System.out.println("SQL Exception: " + sqle.getMessage());
        }

    }

}

Everything compiles just fine, but when I run it, I throw the following SQLException:

SQL Exception: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "coffee"'.

What exactly am I doing wrong? Is there something I can run to otherwise test connectivity between Java and the database? I'm fairly new to both.

Also, it should be noted that this is not homework.

EDIT: It would seem the offending line of code is

 String url = "jdbc:mysql://127.0.0.1:coffee";

and should be changed to

 String url = "jdbc:mysql://127.0.0.1/coffee";

because coffee is a database and not a port.

I guess I need more of it. Thanks to everyone who helped.

3 Answers 3

2

Your connection URL is using coffee as the port. You should use something like:

jdbc:mysql://127.0.0.1:3306/coffee
Sign up to request clarification or add additional context in comments.

Comments

1

Why do you have "coffee" as your port number? You're supposed to have your mysql server port number there

jdbc:mysql://127.0.0.1:coffee

Hint: default port number is 3306. So try

jdbc:mysql://127.0.0.1:3306/coffee

(Assuming your database name is coffee)

1 Comment

Indeed. I've updated the OP to reflect your accurate information. Thank you.
1

I think the parser is getting confused and thinking that coffee is a port number.

Try this:

jdbc:mysql://127.0.0.1/coffee

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.