Reputation: 59
I'm so grateful for the guys who have assisted me with advice on my previous post of the program below. This is my first sql-java (database) program, though I have some basic college knowledge of the two languages. I set my CLASSPATH via environment variable as per advice and YES the FirstExample.java / FirstExample.class is successfully compiled by Java Compiler and recognized at run time of the the JVM in the command line unlike before.
But then while trying to connect to the database I receive a long error message that I can't understand.
I'm a bit suspicious this could bit arising from 'username' issue, though I'm not really sure as I'm a newbie with database programing.
I'm wondering neither in the MySQL configuration wizard nor SQL -database design does 'username' appear though it's part of my tutorial FirstExample.java.
Please assist with solving this problem giving me head ache the whole day, thanks in advance.
Below is the command line error message:
Connecting to database...
java.sql.SQLException: Access denied for user 'username'@'localhost' (using pass
word: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:928)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4736)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1342)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2493)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2
526)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
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(NativeConstruct
orAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
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
:347)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at FirstExample.main(FirstExample.java:22)
Goodbye!
Below is the basic information:
I saved the file as: C:\glassfish3\jdk\jre\lib\FirstExample.java
then compiled to FirstExample.class
CLASSPATH: C:\glassfish3\jdk\jre\lib;C:\Program Files (x86)\MySQL\MySQL Connector J\mysql-connector-java-5.1.27-bin.ja
r
CATALINA: C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\jsp-api.jar
JAVA_HOME: C:\glassfish3\jdk
path: C:\glassfish3\jdk\bin;C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin
MySQL directory: C:\Program Files (x86)\MySQL\MySQL Server 5
.
Here is the database info:
database - 'EMP'.
table - 'Employees'.
password - 'password'
Columns; id; age; first; last.
data; 100; 28; Zaid; Khann.
Java program:
//STEP 1. Import required packages
import java.sql.*; // for standard JDBC programs
public class FirstExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName ( "com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating Statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.print(", Last: " + last);
}
//STEP 6: Clean-up enviroment
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
}catch (Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if (stmt !=null)
stmt.close();
} catch(SQLException se2) {
}// nothing we can do
try{
if(conn !=null)
conn.close();
} catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end FirstExample
This is line 22 of the Java program in code editor:
conn = DriverManager.getConnection(DB_URL,USER,PASS);
Upvotes: 0
Views: 16910
Reputation: 11
Check your username and password, you are using username as username and password as password, I think that your password is correct (because your database info said so), so you can try to change username to root:
static final String USER = "root";
Upvotes: 1
Reputation: 3914
You are using username
as username, password
as password and you are logging in from localhost
. You have to have an entry in the mysql.user
table that corresponds to this.
Use CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'
to create the user you are logging in with.
Upvotes: 0