0

I have started trying out some stuff so that I can use mysql database together with Java. First of all I have some questions about it.

I have used mysql a lot with PHP development but never with Java. Can I use the MySQL that MAMP brings or do I have to install it stand alone or something?

and second.. I have created this code with the help of a tutorial but the only output I get is

com.mysql.jdbc.Driver

The code that I have used for this you can find below:

package Databases;

import java.sql.*;

public class MysqlConnect{

/* These variable values are used to setup
the Connection object */

 static final String URL = "jdbc:mysql://localhost:3306/test";
 static final String USER = "root";
 static final String PASSWORD = "root";
 static final String DRIVER = "com.mysql.jdbc.Driver";

 public Connection getConnection() throws SQLException {
    Connection con = null;
    try {
       Class.forName(DRIVER); 
       con = DriverManager.getConnection(URL, USER, PASSWORD);
    }
    catch(ClassNotFoundException e) {
       System.out.println(e.getMessage());
       System.exit(-1);
    }
    return con;
 }

 public void getEmployees() {
    ResultSet rs = null;
    try {
       Statement s = getConnection().createStatement();
       rs = s.executeQuery("SELECT id, name, job_id, location FROM person");
       System.out.format("%3s %-15s %-7s %-7s%n", 
          "ID", "NAME", "JOB ID", 
            "LOCATION");
       System.out.format("%3s %15s %7s %7s%n", 
          "---", "---------------", 
            "-------", "--------");

       while(rs.next()) {
          long id = rs.getLong("id");
          String name = rs.getString("name");
          long job = rs.getLong("job_id");
          String location = rs.getString("location");
          System.out.format("%-3d %-15s %7d %5s%n", 
             id, name, job, location);
       }
    }
    catch(SQLException e) {
       System.out.println(e.getMessage());
       System.exit(-1);
    }
 }
}
4
  • Include the code that is calling the getEmployees method. Commented Dec 19, 2012 at 20:38
  • where would you even get that print statement to come out? I don't see a logical place for it in your code. Commented Dec 19, 2012 at 20:39
  • Print the stacktrace of the exception and you'll see what's wrong Commented Dec 19, 2012 at 20:41
  • Please do not incompatibly change the question, causing all answers to be invalid. If you have a new question, ask a new question. I've rolledback your invalid edit. Commented Dec 19, 2012 at 21:13

2 Answers 2

7

It's coming from the following block:

catch(ClassNotFoundException e) {
   System.out.println(e.getMessage());
   System.exit(-1);
}

That's a pretty poor way of handling exceptions. You're just printing the exception message. You have no clue what's going on. Rather just throw it (which will end up with a nice stacktrace), or print a more descriptive message along alone the exception message, e.g.

catch(ClassNotFoundException e) {
   System.out.println("JDBC driver class not found in runtime classpath: " + e.getMessage());
   System.exit(-1);
}

How to fix the particular exception is in turn actually a second question (with a pretty obvious answer: just put JAR file containing JDBC driver class in runtime classpath), but ala, you may find this mini-tutorial helpful then: Connect Java to a MySQL database.


Unrelated to the concrete problem, I'm not sure which tutorial you're reading there, but I'd take it with a grain of salt. Apart from poor exception handling, it's also leaking DB resources in getEmployees() method by never closing the result set, statement and connection. This is absolutely not a good practice either. How to do it is also already covered in the aforelinked mini-tutorial. See further also: How often should Connection, Statement and ResultSet be closed in JDBC?

Sign up to request clarification or add additional context in comments.

4 Comments

I use this tutorial: java.blogs.webucator.com/2010/07/05/… and sorry for the poor code, I've just started programming in Java. still learning :) could you answer my question if I just could use the MAMP MySQL?
MySQL server is MySQL server. Your current problem is definitely not caused by an improper MySQL installation. Even more, the code was never been able to come to the step of creating a connection to the DB in question.
Ah Oke i've modified my code a little bit now like in the oracle docs. But I still don't understand where to put the drivers jar file.. i've also changed the code above like it is now.
Just in the runtime classpath. Where exactly that is is also mentioned in the mini-tutorial.
0

Yes, you need to install MySQL server locally or remotely.

The code will be usable if you also downloaded jdbc Driver jar from MySQL download pages. and you configured your MySQL instance with the proper username and password.

2 Comments

Am I not able to use the MySQL that comes with MAMP?
Of course you can use the same MySQL instance. A MySQL database is a MySQL database.

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.