So you’re creating a Java application and want it to work and get connected to a MySQL database. Here’s a quick tutorial on how to get your Java App working with MySQL database.
First, you need to identify your MySQL connection properties like your host address, username, password and database name (assuming that you have your MySQL already up and running.) Also, make sure that you have your Java MySQL Connector installed in your Java JDK library path. You can download Java MySQL Connector here, if you do not have one.
Now, having those information, you need to initiate a new instance of the Java MySQL Connector using these lines:
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
conn = DriverManager.getConnection (ConnectionURL, dbUsername, dbPassword);
You can replace the parameters for the connection for example:
String ConnectionURL = “jdbc:mysql://hostAddress/dbName”;
… where hosAddress is your MySQL host address and dbName for your database name. For example, if you have your MySQL running under localhost (or IP Address if you have it somewhere else) and you want to connect to accounts database, you can connect with this:
String ConnectionURL = “jdbc:mysql://localhost/account”;
Read the rest of this entry »