3

Hello as Im new to ubuntu i want to learn Mysql database with Java,So how to install the software and configure them for the Programming in ubuntu 13.04

1 Answer 1

5

You'll want to take a look at the JDBC

Setup MySQL

First things first you'll need to setup your MySQL. Install the mysql server and client:

sudo apt-get install mysql-server mysql-client

(This will install both in the most up to date version within your repositories.)

You will be prompted for a root password. Set this to something secure; the root account can access and change all databases.

You then need to login and setup the server:

mysql -u root -p

You will be prompted for your password and then connected to the server. You need to setup a database (a database contains tables):

CREATE DATABASE [name];

Some syntax to note in MySQL is that it is case insensitive (you can just write out quickly in lower case as I do, though for clarity in code it's wise to write in capitals with statements) and also you must end every statement with a semi colon.

Switch to this database:

USE [name];

Now you want to add a user and set permissions to allow the user to edit everything on this database (docs):

CREATE USER "[username]"@"[host]" IDENTIFIED BY "[password]";
GRANT ALL PRIVILEGES ON * TO "[username]"@"[host]";

The username is obviously the desired username for the user which will access this database from your java program. The host in both cases is the host which you access from, use % to be unspecified. The password will be converted to a hash and stored to identify the user. As far as I know there isn't much risk in an insecure password here since this user can only access and modify tables on this localhost.

Now that this user is created exit with exit or quit commands, and login as this user.

quit
mysql -u [username] -p
<enter password>
USE [databasename];

Now you'll want to setup your table, for this you'll want to have a good read on SQL, w3schools do a great tutorial

Code with JDBC

Now for the fun part - the Java code.

Setting up the project is fairly simple, open up a new project as you'd normally do. Add the mysql connector jar to the project files and add it to the build jars (in eclipse put the jar in your folder within the workspace, right click on the project, properties, then select "Java Build path", add jars or add externel jars and navigate to the MySQL connector jar.

I wont describe using the JDBC in depth since the tutorial on the MySQL development site is fairly comprehensive.

These are the basics though:

private Connection openConnection(String url, String user, String password) {
    Properties properties = new Properties();
    properties.put("user", user);
    properties.put("password", password);
    properties.put("characterEncoding", "UTF-8");
    properties.put("useUnicode", "true");

    Connection c;
    try {
        //Class.forName("com.mysql.jdbc.Driver").newInstance();
        c = DriverManager.getConnection(url, properties);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return c;
}

...

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection sqlCon = openSQLCon("jdbc:mysql://[mysqlhost]:3306/[databasename]", "[username]", "[password]");

Statement st = sqlCon.createStatement();
st.execute("SQL STATEMENT");

sqlCon.close();

This will connect to the MySQL database given previously mentioned values (username, database, password, and the host will be the host machine of the MySQL server. Also to note that 3306 is just the default port for MySQL and so might be different if you set it so.) It will then execute the "SQL STATEMENT" and close the database. You will need to catch exceptions, and of course handle results sets. You will need to import several thing from "java.sql", and also "java.util.Properties". Information on all of this is in the JDBC docs.

You should spend some time reading the tutorials with JDBC and MySQL, and play around with them. Most importantly have fun!

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .