This tutorial only shows briefly the steps the construction of Java database applications that connect to the MySQL server. To be able to connect to MySQL from Java is necessary JDBC driver for MySQL. Please download at the website mysql.com, obtained the file mysql-connector-java-5.1.8.tar (latest date). Then extract the file. One result of extraction is the file mysql-connector-java-5.1.8-bin.jar and this is the MySQL JDBC driver in question. Copy this file to the directory C: Program FilesJavajre6libext or to another folder according to the location of the JRE installation.
As the use of other languages, an important step in making a database application is to create a database and then access it from the language used.
How to create a database in MySQL? The easiest way is to use web tools such as PHPMyAdmin. If no, we can directly into the console and run the mysql client program. The steps below are easy to follow:
1. Go to the console (Command Prompt).
2. Change to the directory where the mysql program is located. If using XAMPP then mysql are in C: xamppmysqlbin (adjust to conditions in your computer).
3. Run the mysql command, for example: mysql-u root
4. At the mysql> prompt, type create database feedback; (end with Enter). This will generate a new database called feedback.
5. Give commands use the feedback; for feedback database becomes the active database
6. Add new users into the system, for example sqluser. Give permission to users to access the feedback database earlier.
Sqluser CREATE USER IDENTIFIED BY 'sqluserpw';
grant usage on *.* to sqluser @ localhost IDENTIFIED BY 'sqluserpw';
grant all privileges on feedback .* to sqluser @ localhost;
7. Create a table called comments by some fiels in it as shown below:
CREATE TABLE COMMENTS (id INT NOT NULL AUTO_INCREMENT,
Myuser VARCHAR (30) NOT NULL,
EMAIL VARCHAR (30),
WebPage VARCHAR (100) NOT NULL,
Datum DATE NOT NULL,
SUMMERY VARCHAR (40) NOT NULL,
VARCHAR COMMENTS (400) NOT NULL,
PRIMARY KEY (ID)
);
8. Enter a line of records into the table.
INSERT INTO COMMENTS values (default, 'boots', 'myemail@gmail.com', 'http://www.vogella.de',
'2004-06-22 10:33:11 ',' Summery ',' Na das war wohl nicths');
Making a database, tables and initial data mengisian been done. Now it's time to write Java programs to access the MySQL database.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
public class DaoMySQL {
private Connection connect = null;
private Statement statement = null;
private ResultSet resultset = null;
public DaoMySQL () throws Exception {
try {
Class.forName ("com.mysql.jdbc.Driver"). NewInstance ();
connect = DriverManager.getConnection (
"Jdbc: mysql: / / localhost / feedback?" +
"User = & password = sqluser sqluserpw");
PreparedStatement statement = connect.prepareStatement ("SELECT myuser," +
"Webpage, datum, summery, COMMENTS from FEEDBACK.COMMENTS");
ResultSet = statement.executeQuery ();
while (resultSet.next ()) {
String user = resultSet.getString ("myuser");
String website = resultSet.getString ("webpages");
String resultSet.getString summery = ("summery");
Date date = resultSet.getDate ("datum");
String comment = resultSet.getString ("comments");
System.out.println ("User:" + user);
System.out.println ("Website:" + website);
System.out.println ("Summery:" + summery);
System.out.println ("Date:" + date);
System.out.println ("Comment:" + comment);
}
} Catch (Exception e) {
throw e;
} Finally {
close ();
}
}
private void close () {
try {
if (resultset! = null) {
resultSet.close ();
}
if (statement! = null) {
statement.close ();
}
if (connect! = null) {
connect.close ();
}
} Catch (Exception e) {}
}
public static void main (String [] args) throws Exception {
DaoMySQL DaoMySQL dao = new ();
}
}
Java programs on top of just taking the contents daritabel Comments and displays it to the console.