How to Set Up MySQL/JDBC on Ubuntu


What is JDBC?

JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.

The JDBC library includes APIs for each of the tasks commonly associated with database usage:
Making a connection to a database
Creating SQL or MySQL statements
Executing that SQL or MySQL queries in the database
Viewing & Modifying the resulting records

What is MySQL?

MySQL is an open source relational database management system (RDBMS) based on Structured Query Language (SQL).

MySQL runs on virtually all platforms, including Linux, UNIX, and Windows. Although it can be used in a wide range of applications, MySQL is most often associated with web-based applications and online publishing and is an important component of an open source enterprise stack called LAMP.

Setting Up MySQL:

Run the following command in the terminal:
sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install libmysql-java
This will install both in the most up to date version within your repositories. You will be prompted for a root password. You then need to login and setup the server. To do that, run the following command:
mysql -u root -p
Password will be asked and after that you will be connected to the server. You have to create a database to work on. Run the following command to create one:
CREATE [database_name];
Replace [database_name] with the name you want. Now, we have to switch database. Run following command in the terminal:
USE [database_name];

Now, we have to set classpath. Enter this command at the end of you bashrc file:
export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar

Setting Up IDE:

In you favorite IDE, go to Project Properties tab, and click “Add External Jars”. Choose the jar file located at “/usr/share/java/” directory, in this case “mysql-connector-java.java”. Now test this connection using this program:
import java.sql.Connection;
import java.sql.DriverManager;

class JDBCTest {

    String url = “jdbc:mysql://localhost”;

    String user = “username”;

    String password = “password”;

    public static void main(String args[]) {

        try {
            Connection con;
            con = DriverManager.getConnection(url, user, password);
            System.out.println(“Success”); 
        } catch (Exception e) {
            e.printStackTrace(); 
        }
    }
}
After running this program, “Success” message will be shown if the connection is sucessful.
Got problems? Let me know in the comments.
How to Set Up MySQL/JDBC on Ubuntu How to Set Up MySQL/JDBC on Ubuntu Reviewed by Chirag Jain on Wednesday, July 08, 2015 Rating: 5

5 comments :

  1. root@westcoast:~# javac javadb.java
    javadb.java:16: error: non-static variable url cannot be referenced from a static context
    con = DriverManager.getConnection(url, user, password);
    ^
    javadb.java:16: error: non-static variable user cannot be referenced from a static context
    con = DriverManager.getConnection(url, user, password);
    ^
    javadb.java:16: error: non-static variable password cannot be referenced from a static context
    con = DriverManager.getConnection(url, user, password);
    ^
    3 errors

    ReplyDelete
    Replies
    1. I know I'm late... but for others looking for troubleshoot for this problem, just add the keyword "static" before the String declarations. :-)

      Delete
  2. Error: Could not find or load main class JDBCTest.class

    ReplyDelete
  3. Error:com.mysql.jdbc.Driver

    ReplyDelete

Powered by Blogger.