Wednesday, 22 January 2014

A Simple Java Class for a MySQL Database

Hello , today i will share with you a Simple Java class used for MySQL aware programs . This class will help you to understand and use simple MySQL queries : select and insert .

You can download the source code here !!!

Before you proceed you will need to : 

  • Install MySQL server
  • Create a user and a password to use
  • Create a  MySQL database : in the mysql console write the following with out the quotes : "CREATE DATABASE dbExample; " then "USE dbExample; " now you will create a simple table to perform your tests " CREATE TABLE myTable (
    id int not null auto_increment,
    name varchar(30) not null,
    surname varchar(30) not null,
    phone varchar(30) not null,
    email varchar(30) not null,
    primary key (id));"
  • Now we have a database and a Table but we have no Data inside :( lets make some data for our database using Java!  (i use NetBeans by the way ) 
  • Unzip the file you downloaded if not already :D
  • Click Open a Project, find and select sqlProgramm
  •  Add the MYSQL JDBC DRIVER in case it does not work : right click on Libraries (in your project) add Library
  • Now you are ready to perform queries using Java: in the SqlProgramm class write : 

  •  Now lets Make a Select all query :

you will see all the data stored in the table :)

Enjoy !!!

Note : You can edit the username and password for MySQL in the sqlQuery class 

In the next Tutorial we will have more functionality :
delete and update  

No comments:

Post a Comment