0

I am building my own CRM(Customer Relationship Manager) application using a mysql database as storage, I now am trying to link my Eclipse Java project to the database so the tables become visible. I'm trying to run this code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import java.util.Date;

public class MySQLclass{
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;

public void readDataBase() throws Exception {
try {
  // This will load the MySQL driver, each DB has its own driver
  Class.forName("com.mysql.jdbc.Driver");
  // Setup the connection with the DB
  connect = DriverManager
      .getConnection("jdbc:mysql://localhost/test"
          + "user=root&password=password");

  // Statements allow to issue SQL queries to the database
  statement = connect.createStatement();
  // Result set get the result of the SQL query
  resultSet = statement
      .executeQuery("select * from test.customers");
  writeResultSet(resultSet);

  // PreparedStatements can use variables and are more efficient
  preparedStatement = connect
      .prepareStatement("insert into  test.customers values (default, ?, ?, ?, ? , ?,        ?, ?)");
  // "myuser, webpage, datum, summary, customers from test.customers");
  // Parameters start with 1
  preparedStatement.setString(1, "Sjaakie Sjaakssen");
  preparedStatement.setString(2, "Sjaakssen@yahoo.nl");
  preparedStatement.setString(3, "pannekoekenstraat 2");
  preparedStatement.setString(4, "Rotterdam");
  preparedStatement.setString(5, "3022PK");
  preparedStatement.setString(6, "0101235489");
  preparedStatement.setString(7, "Yahoo");
  preparedStatement.executeUpdate();

  preparedStatement = connect
      .prepareStatement("SELECT NAAM, EMAIL, TELEFOON, BEDRIJFSNAAM, customers from test.customers");    
  resultSet = preparedStatement.executeQuery();
  writeResultSet(resultSet);

  // Remove again the insert comment
  preparedStatement = connect
  .prepareStatement("delete from test.customers where NAAM= ? ; ");
  preparedStatement.setString(1, "Test");
  preparedStatement.executeUpdate();

  resultSet = statement
  .executeQuery("select * from test.customers");
  writeMetaData(resultSet);

} catch (Exception e) {
  throw e;
} finally {
  close();
}

}

private void writeMetaData(ResultSet resultSet) throws SQLException {
//   Now get some metadata from the database
// Result set get the result of the SQL query

System.out.println("The columns in the table are: ");

System.out.println("Table: " + resultSet.getMetaData().getTableName(1));
for  (int i = 1; i<= resultSet.getMetaData().getColumnCount(); i++){
  System.out.println("Column " +i  + " "+ resultSet.getMetaData().getColumnName(i));
 }
}

private void writeResultSet(ResultSet resultSet) throws SQLException {
// ResultSet is initially before the first data set
while (resultSet.next()) {
  // It is possible to get the columns via name
  // also possible to get the columns via the column number
  // which starts at 1
  // e.g. resultSet.getSTring(2);
  String Naam = resultSet.getString("naam");
  String Email = resultSet.getString("email");
  String Adres = resultSet.getString("adres");
  String plaats = resultSet.getString("plaats");
  String Postcode = resultSet.getString("postcode");
  String Telefoon = resultSet.getString("telefoon");
  String Bedrijfsnaam = resultSet.getString("bedrijfsnaam");
  System.out.println("Naam: " + Naam);
  System.out.println("Email: " + Email);
  System.out.println("Adres: " + Adres);
  System.out.println("plaats: " + plaats);
  System.out.println("Postcode: " + Postcode);
  System.out.println("Telefoon:" + Telefoon);
  System.out.println("Bedrijfsnaam" + Bedrijfsnaam);
  }
    }

     // You need to close the resultSet
 private void close() {
    try {
      if (resultSet != null) {
        resultSet.close();
      }

      if (statement != null) {
        statement.close();
      }

      if (connect != null) {
    connect.close();
      }
    } catch (Exception e) {

    }
  }

} 

with this main class:

public class Main {
  public static void main(String[] args) throws Exception {
    MySQLclass dao = new MySQLclass();
dao.readDataBase();
  }


} 

And i keep getting this error:

Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:927)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1709)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1252)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2483)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MySQLclass.readDataBase(MySQLclass.java:22)
at Main.main(Main.java:5)

Could someone please shed some light on my dilemma.

user2674895
  • 39
  • 1
  • 3
  • 9
  • This is probably nothing to do with Java. Does it work with command line? Try GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'root' WITH GRANT OPTION; See more at http://stackoverflow.com/questions/11922323/java-sql-sqlexception-access-denied-for-user-rootlocalhost-using-password – Jayan Aug 12 '13 at 12:39
  • I connected to my database using command line,NickJ's solution helped but it was followed by a different error – user2674895 Aug 12 '13 at 12:55
  • 1
    @ user2674895 : Your question started with one and then became another issue. This reduces its usefulness for future vistor. Voting to close as duplicate. – Jayan Aug 13 '13 at 04:35
  • This question appears to be off-topic because it is about a simple SQL syntax error – Andrew Barber Aug 21 '13 at 14:30

3 Answers3

4

The clue is in the error:

Access denied for user ''@'localhost'

It thinks the username is '' (empty string) In the database URL, you forgot to separate the path in the URL from the parameters with a question mark.

Try this:

connect = DriverManager
  .getConnection("jdbc:mysql://localhost/test?"
      + "user=root&password=MSQLpass21!");
NickJ
  • 9,380
  • 9
  • 51
  • 74
  • Wow, this worked!!! it shows the fields in my database, followed by a different kind of error. I triet to vote up but i need 15 reputation :P, BUT, this is progress and i thank you! – user2674895 Aug 12 '13 at 12:52
  • `Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'customers' in 'field list' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) ` – user2674895 Aug 12 '13 at 12:56
  • But its better to have user name and password as different arguments, see my answer. – venkat balabhadra Aug 12 '13 at 16:22
0

You could use public static Connection getConnection(String url, String user, String password) to get connection using user name and password.
Or try out connect = DriverManager.getConnection("jdbc:mysql://localhost/test?" + "user=root&password=MSQLpass21!");

0

Your second error is probably caused by this line:

SELECT NAAM, EMAIL, TELEFOON, BEDRIJFSNAAM, customers from test.customers

if customers is a table name you cannot use it in the select clause. Either use select * or use all the specific field names you want to fetch:

SELECT NAAM, EMAIL, TELEFOON, BEDRIJFSNAAM from test.customers
c.s.
  • 4,786
  • 18
  • 32