I have written some java code to connect to a database I have created in mysql.
I have never used mysql before. I just installed, and ran "sudo mysql", which then allowed me to create a db, and add a table.
The java code to connect to the db is as follows....
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb");
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
When I try to run this code, an exception is thrown, and printed out as follows...
SQLException: Access denied for user ''@'localhost' (using password: NO) SQLState: 28000 VendorError: 1045
I looked at the apis and one of the getConnection variants take a username and password, which I supplied root and my root password. This didn't work either.
But more than this specific error, I am not just not sure how to approach user and passwords when connecting to mysql from java on linux. Is it really acceptable to put my root password in a string literal in code? Should I always create a new separate user other than root, to connect to the database? Is there a more secure place where I can put my user and password, to connect to mysql?