Re: Logging in to MySQL
23.10.2009 12:47, Roedy Green kirjoitti:
I seems to me I was able to login to a MySQL database some years ago,
but now for the life of me I can't get the simplest code to work.
Here is my SSCCE. I can access the database fine from the command
line with mysql -uroot -psesame
You forgot to load the driver.
public class DBDemo
{
// The JDBC Connector Class.
private static final String dbClassName = "com.mysql.jdbc.Driver";
// Connection string. emotherearth is the database the program
// is connecting to. You can include user and password after this
// by adding (say) ?user=paulr&password=paulr. Not recommended!
private static final String CONNECTION =
"jdbc:mysql://127.0.0.1/emotherearth";
public static void main(String[] args) throws
ClassNotFoundException,SQLException
{
System.out.println(dbClassName);
// Class.forName(xxx) loads the jdbc classes and
// creates a drivermanager class factory
Class.forName(dbClassName);
// Properties for user and password
Properties p = new Properties();
p.put("user","paulr");
p.put("password","paulr");
// Now try to connect
Connection c = DriverManager.getConnection(CONNECTION,p);
System.out.println("It works !");
c.close();
}
}
--
You're at the end of the road again.