Mysql and Java: 1 Requirements
Mysql and Java: 1 Requirements
Mysql and Java: 1 Requirements
1 Requirements
You will need:
2. MySQL installed. Directions for installing MySQL on CSIF machines can be found at
http://csifdocs.cs.ucdavis.edu/tiki-index.php?page=CSIF+MySQL+4.x+Install
3. Start the mysql client. We will use the command line client to create a new database,
a new user and a table in the new database.
1
(a) $ mysql -u root -p
Welcome to the MySQL monitor. Commands end with ; or \g.
..
.
mysql>
(b) Create a new database named ecs160tutorial.
mysql> CREATE DATABASE ecs160tutorial;
Query OK, 1 row affected (0.06 sec)
(c) Create a user with all privileges on this database. The user name will be tutorialuser
and the password will be 123456. Although this is NOT good practice, it will
suffice.
mysql> GRANT ALL ON ecs160tutorial.* TO tutorialuser@’%’
-> IDENTIFIED BY ’123456’;
mysql> GRANT ALL ON ecs160tutorial.* TO tutorialuser@’localhost’
-> IDENTIFIED BY ’123456’;
(d) Quit mysql client. We’ll reconnect as tutorialuser to the ecs160tutorial
database to setup a table.
mysql> quit
Bye
$ mysql -u tutorialuser -p ecs160tutorial
Enter password:
Welcome. . .
..
.
mysql>
(e) Now we will create a simple table with two columns, name and last name.
mysql> CREATE TABLE simple_table (name CHAR(128), last_name CHAR(128));
Query OK, 0 rows affected (0.01 sec)
show tables command will list all the tables created in this database.
mysql> show tables;
+--------------------------+
| Tables_in_ecs160tutorial |
+--------------------------+
| simple_table |
+--------------------------+
1 row in set (0.00 sec)
So far we set up a new database for this tutorial, created a user and a very simple table.
I think it is a good idea to create a new database and user for your project as we did in this
tutorial. In the next section, I’ll describe how to connect to the ecs160tutorial database
from a Java program and execute simple queries.
2
3 Connecting to a MySQL database from a Java pro-
gram using the Connector/J JDBC driver
I assume that you downloaded and installed Connector/J. If you haven’t done so, read
section 1 for the requirements.
You can connect to the MySQL database in two steps. Those steps are detailed below.
Class driver_class=null;
try {
driver_class = Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Found driver " + driver_class);
We don’t need to register the driver, once it is loaded it will be used for connection
requests to mysql databases.
2. Next step is to connect to the MySQL server and the ecs160tutorial database. Recall
that the user name is tutorialuser and the password is 123456.
Connection connection=null;
try {
connection = DriverManager.getConnection
("jdbc:mysql://localhost:3306/ecs160tutorial","tutorialuser","123456");
} catch (SQLException e) {
e.printStackTrace();
}
try {
System.out.println
("Established connection to "+ connection.getMetaData().getURL());
} catch (SQLException e1) {
e1.printStackTrace();
}
3
replace 3306 with the number of the port your MySQL server is listening on. Next
component of the URL is the database name. The second argument is the user name
and the last one is the password.
3. Run the following query, the output you see should be similar to the output given
below.
Now, we will execute the same SELECT query from our Java program.
statement = connection.createStatement();
4
ResultSet resset = statement.getResultSet();
4. We are ready to print the result of the query. The result set returned by the statement
initially points before the first row, thus you must call next to advance to the first
row. See the code snippet below.
A row of the result set is made up of columns. We know the column names and the
types of the columns of simple_table; they are name and last_name and both are
type string. Therefore, we will use getString (remember column type) method with
the column names.
The output should be similar to the one below.
4 Summary
In this tutorial I explained, using MySQL, how to create a database, a user, and a simple
table. I also explained how to connect to a MySQL database from a Java program and
execute queries. The Java program I used as the example can be found in the appendix.
You can use javac to compile the program. Don’t forget to change the host address and the
port number. To run it, you will need to pass -classpath option:
java -classpath /home/<user_name>/mysql-connector-java-3.1.12-bin.jar:$CLASSPATH:. Main
5
RSS
S T MUM VXW8YZ
SR
HIJ$K '*3!342 " /4[ G$!40 \ ]^
$_a` bdce!7U;='EfgP
h JM=NN 9 [ 784a"17.i^2+!''dj_H*JJ,5
kmlnP
[784a"197i^2+!=''oj h JMNN % p/7Uq!#G$1 \ rD2*/G&%Gs='*)+t%(*[u2v%.6784a"197#r f$5
w K*MKxy\ h JM=NN*z{*H$_a|=}d~ K*#*=_ 19fgP
1t% FX784$0*3?3!2$8v7U!2*1 \*f5
U$$HXd_5
w
]alEN# % />*3 % Fk78403+0 \ rad/>0*[L[9784a"17Lr [974*"$197.ik2#+#!=''Ef5
kmlnP
]alEN* % />3 % Fk74$03+0 \ rDB'*3!u#+4'#1[2*/00*12*34#/03/r
2/00123$4#/0% ;13#:$13!6!3! \fE% ;13$<A \*ff5
w K*MKxy\ ]#E}d~ K*#*=_ 1E=fgP
1 9% Fk78403?3!2,7U!21 \f5
w
w
w
¦ \U2/00*123$4#/0«§¬j¤_HJJf
P
^mlnP
2/00123$4#/0% 2+/='a1 \*f5
w KaM$Kxª\ ]X}D~EKa#$_ 1fgP
1&% Fk78403?3!2,7U!21 \f5
w
w
w
w