Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Sunday, July 24, 2011

OpenJPA Components

Earlier I have explained about the concept of openJPA & its usage in the programming environment. So before going into development, let me describe about the architectural components in openJPA.
if we consider a web based project using openJPA, it will have web pages, source packages, libraries & configuration files.a typical openJPA based netbeans project folder structure would look like this.

  1. In web pages we include all our views. that means in MVC architecture the View is the web pages. So .html, .jsp files goes to the web pages.
  2. In source packages you need to define the packages required. In standard openJPA based development environment, you need to have following packages.
  • DAO - Data Access Object
DAO means objects that provides an abstract interface to databases. Separate logic within 2 parts of application can be done using same DAO interface & then changes does not affect to DAO clients. Basically what it does is separating the data access logic from business logic. Also DAO provides CRUD operations (Create/ Read/ Update/ Delete).
  • Domain
 In the domain, we include the database object files. That means if we create a database having two tables, we need to import the table objects to the domain package. For example if we have a table called "employer" then in the domain package there will be a java file called employer.java containing the table objects.
  • Service
In services package we define the mapping between DAO to web pages.

  3.   In Libraries you need to include all the jar files & other libraries required for the project. For example, in OpenJPA based project it will contain MYSQL JDBC Driver, persistence ejb-persistence.jar, openjpa-all-2.1.0.jar, tomcat 6.0, etc.

  4.  In the configuration files the important file is the Persistence.xml which holds the entire openJPA structure. persistence.xml is to define the service providers & the services to be provided. basically it will contain following details.
So I just mentioned the important things you need to know before starting a openJPA project. In the next post let's try this out & develop a MVC based project.


Monday, June 27, 2011

How to Connect JDBC Driver to a Java Program

Well, when you do programming, its quite common to work with databases. So many people use mysql as their database in most of the cases. So I thought it will be useful for the newcomers to learn how to connect mysql to a java program.

Assuming you have installed mysql in your machine lets look at how we write our codes.
or here are the links for you to get them. :)
Get JDBC Driver for Ubuntu
MySQL-Query-Browser

First you need to create a database for your purpose.

Here is the code for creating a database using mysql -

In Query Browser
CREATE DATABASE testdb;

in phpmyadmin
CREATE DATABASE testdb;
USE testdb;

Then you need to define the tables. Lets create a table called Student including student id, name, address as the fields.

create table student(
stu_id int (2),
stu_name varchar (20),
stu_address varchar (50)
);

Now you need to open your IDE, in my case its Netbeans.

Now create a New Java project giving a name - i.e. testdb
Now goto Libraries in the left panel & right click to get the menu & select add a librabry
From the menu select  
MYSQL JDBC Driver - mysql-connector-java-5.1.6-bin.jar
(you need to download this first from the site)

Then in the java file you need to import below package.
import java.sql.*;

Now within the main method, write these codes

      Connection con = null;
       String url = "jdbc:mysql://localhost:3306/testdb";
       String user = "root";
       String password = "root";


Here you should give your database username & password.
Then within a try block you need to write this.
          con = DriverManager.getConnection(url, user, password);
           Statement st = con.createStatement();


Now you are ready to write queries to insert/upadate & delete records in the table.

1. Insert data to the Database table.

         String sql = ("INSERT student VALUES   ('1','Saman','Colombo')");                                   
      st.executeUpdate(sql);
          System.out.println("Entered");

2. Selecting & Displaying Results

 ResultSet rs = st.executeQuery("SELECT * FROM student WHERE user_id ='1'");       

   while(rs.next())  {      
           int id = rs.getInt("userstu_id");          
          String name = rs.getString("stu_name");       
          String address = rs.getString("stu_address");     
          System.out.println("ID :" + id);        
         System.out.println("Name :" + name);   
         System.out.println("Address :" + address);         
}

3. Deleting Data from the Table

String sql1 = ("DELETE FROM user_info");
st.executeUpdate(sql1);

So hope this little push would help you out guyz. Try more things your own :)