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);
    }
 }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Reshad
  • 2,570
  • 8
  • 45
  • 86
  • Include the code that is calling the `getEmployees` method. – Perception Dec 19 '12 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. – Woot4Moo Dec 19 '12 at 20:39
  • Print the stacktrace of the exception and you'll see what's wrong –  Dec 19 '12 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. – BalusC Dec 19 '12 at 21:13

2 Answers2

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?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I use this tutorial: http://java.blogs.webucator.com/2010/07/05/java-database-connectivity-jdbc-basics-using-mysql/ 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? – Reshad Dec 19 '12 at 21:04
  • 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. – BalusC Dec 19 '12 at 21:06
  • 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. – Reshad Dec 19 '12 at 21:10
  • Just in the runtime classpath. Where exactly that is is also mentioned in the mini-tutorial. – BalusC Dec 19 '12 at 21:11
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.

Michael
  • 2,827
  • 4
  • 30
  • 47