Posts Tagged ‘database’
Setting Up MySQL/JDBC Driver on Ubuntu
Assuming that you already have MySQL installed, the next step is to install the connector driver. You can do this easily on the CLI by using the following command:
sudo apt-get install libmysql-java
The next step is to make sure that the classpath is set. You can have this set automatically by adding this command to you bashrc file.
export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar
If you want to set this for all users, you should modify the /etc/environment instead.
For those using Eclipse, you can also do this by going through the following steps:
- Select Project Properties > Java Build Path
- Select Libries tab
- Click Add External Jars
- Choose the jar file, in this case mysql-connector-java.java
Once you’re done, you can test the connection using the following snippet:
import java.sql.Connection;
import java.sql.DriverManager;
class JDBCTest {
private static final String url = "jdbc:mysql://localhost";
private static final String user = "username";
private static final String password = "password";
public static void main(String args[]) {
try {
Connection con = DriverManager.getConnection(url, user, password);
System.out.println("Success");
} catch (Exception e) {
e.printStackTrace();
}
}
}