Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL Connection

Status
Not open for further replies.

zinja

MIS
Nov 14, 2002
149
US
I am very new to Java and am trying to learn how to connect and modify a MySQL database. Here is the connection script I am using to try to create a table in an existing database.

What am I doing wrong? I don't get any errors (I don't get any output at all).

Code:
import java.sql.*;
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 


public class CreateCoffees {
    public static void main(String args[]) {
        String url = "jdbc:mysql:MyServerAddress/coffeebreak ";
        //Connection con;
        String createString;
        createString = "create table COFFEES " +
                            "(COF_NAME VARCHAR(32), " +
                            "SUP_ID INTEGER, " +
                            "PRICE FLOAT, " +
                            "SALES INTEGER, " +
                            "TOTAL INTEGER)";
        Statement stmt;

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }

        try {
            Connection conn = DriverManager.getConnection(url, "myUName", "MyPassword");
            System.out.println("Connected");
            stmt = conn.createStatement();
            stmt.executeUpdate(createString);
            stmt.close();
            conn.close();
			System.out.println("Connection Closed");

			
        } catch(SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
    }
}

I am thinking I am not including the connection library properly (although no complaints from XCode (I am using a Mac). The MySQL Server I am attempting to connect with is not local.

Any help would be appreciated, and any advice on where to go to get insight into database connections with Java and MySQL would help. I am an experienced PHP, Javascript, Perl programmer, but Java is new to me.

LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
Where are you supposed to get the output? I'd include a System.out and System.err at the very beginning of the main method to ensure you can see the output.

Cheers,
Dian
 
Output is supposed to be on the screen (XCode pops up a window with any screen output). Also, there is nothing under the Errors & Warnings log.

Thanks,


LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
No, "is supposed to" means it is supposed to, but isn't.

Thanks,


LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
Did you include the output at the beginning of main? If so, I guess it's a problem with XCode.

Cheers,
Dian
 
It was a problem with not specifying the port.

Here is what works:

Code:
String url = "jdbc:mysql://MyServerAddress:3306/coffeebreak";
Instead of
Code:
String url = "jdbc:mysql:MyServerAddress/coffeebreak ";

Thanks for the help though!



LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
That doesn't explain why the output is not showing. Without it, it will be almost imposible to debug future errors.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top