Thursday, May 26, 2011

CONNECTION TO DATABASE, metadata

| Thursday, May 26, 2011 | 0 comments

CONNECTION TO DATABASE, metadata
Project for which we can make to a high with the database we have, then we have toconnect your database with an existing project.
Process connection:
a. Registration drivers
b. Connection to the database
c. Both must exist in the instruction block try {} catch (Exception) {}
Class / Interface in use:
Class Class
Method:
static Class forName (String namaclass) à Driver Registration class
Ex:
Class c = Class.forName ("com.jdbc.mysql.Driver");
All classes used for database management, is in the package java.sql .*
Class DriverManager
Method:
Connection getConnection (String location, String username, String pwd)
Ex:
String location = "jdbc: mysql: / / localhost / nama_db";
String uname = "username";
String pwd = "password";
Connection con = DriverManager.getConnection (location, uname, pwd);
À Connection Interface
Exception:
È Class ClassNotFoundException Exception will occur if there is an error registrationclass,
use forName method of class Class.
Class SQLExceptionèException to know the error connection and access the database / table.
Example 1
Create a new project, add the object form with the draft as follows:

CONNECTION TO DATABASE, metadata

In the TEST CONNECTION button to add the following code:

CONNECTION TO DATABASE, metadata

Adding libraries to the project in a way MYSQL, right-click Libraries choose AddLibrary

CONNECTION TO DATABASE, metadata

Selection dialog will appear as follows:

CONNECTION TO DATABASE, metadata

Select the MySQL JDBC Driver, and click the Add Library.
Run.
Example 2.
If the project that we make involve many forms, and each form must connect to the database, it would be better if the connection is made ​​in a separate class so that wedo not need to write the connection process again and again.
Create a project, add the Form as in Example 1 as follows:

KONEKSI KE DATABASE, METADATA

In this project also add an object class (Select the Java Class, not the Java Form), andcreate a constructor in class for the connection process as follows:

KONEKSI KE DATABASE, METADATA

In the form that had previously designed to add the following event CONNECTIONTEST button, and run.

KONEKSI KE DATABASE, METADATA

Example 3
Process connection can also be placed in a different package with the form.
Create a new project, add a form similar to Example 1 and Example 2 earlier.

KONEKSI KE DATABASE, METADATA

At the Source Package, Right-click the package name is the same with your project,select New à Java Package

KONEKSI KE DATABASE, METADATA

Dialog will appear to fill in the name of the package. Replace newpackage with data.Click the Finish button.

KONEKSI KE DATABASE, METADATA

Will form a new package by name tigakoneksi.data can be seen as follows:

KONEKSI KE DATABASE, METADATA

Right-click the new package, add a Java Class object, write the following code:

KONEKSI KE DATABASE, METADATA

In draft form, add the following event for the button CONNECTION TEST 2:

KONEKSI KE DATABASE, METADATA

If the error sign appears, right click on the lines of program code, select Fix Imports.Will automatically add the name of a new package that we have the following:

KONEKSI KE DATABASE, METADATA

Run.
Metadata DATABASE
Metadata à data about data
Interface used:
Interface Connection
Method:
Statement createStatement ()
à create a Statement object (interface Statement) to send commands to the database query
DatabaseMetaData getMetaData ()
à get the metadata that is stored to the object DatabaseMetaData (DatabasemetaData interface) from an existing database on the Connection object.
DatabaseMetaData Interface
Method:
GetTables ResultSet (String catalog, String schema, String tablename, String [] type)
à find out information from the database tables that exist in the metadata
DatabaseMetaData.
GetColumns ResultSet (String catalog, String schema, String tablename, String nmcolumn)
à know the names of a table from the database field whose metadata is in
DatabaseMetaData.
ResultSet Interface
Method:
String getString (String nmcolumn)
à take the value of the designated column from a ResultSet
Metadata from a database:
1. Get metadata from the connection and save it to DatabaseMetaData
DatabaseMetaData dbmd = con.getMetaData ();
2. Get information from databasemetadata table with the function and save it to a ResultSet getTables
String [] type = {"TABLE"};
ResultSet rs = dbmd.getTables (null, null, null, type);
3. Show information of each table with a loop (rs.next ()) as follows:
a. Find the name of table
String nm_tbl = rs.getString ("TABLE_NAME");
b. Find a table structure
rs1 = dbmd.getColumns (null, null, nm_tbl, null);
c. Show the structure of each table with a loop (rs1.next ()) as follows:
String nm_field = rs1.getString ("COLUMN_NAME");
String tp_field = rs1.getString ("TYPE_NAME");
String lb_field = rs1.getString ("COLUMN_SIZE");
Examples
Displays the name of all tables in the database.
Create a new project, add the object form with the draft as follows:

KONEKSI KE DATABASE, METADATA

In the model properties (JTable) turned into a Custom Code.
Add a new package, in this package add the Java Class object, write code to connect to the database.
Write the following instructions are also on the source form above.

KONEKSI KE DATABASE, METADATA

Run, will be obtained as follows:

CONNECTION TO DATABASE, metadata

Metadata from a ResultSet
Interface is involved:
Interface Statement
Method:
ResultSet executeQuery (String sql)
à execution that produces a ResultSet query (Query SELECT)
ResultSet Interface
Method:
ResultSetMetaData getMetaData ()
à get the ResultSet metadata.
Interface ResultSetMetaData
Method:
getColumnCount int ()
à get the number of columns of a ResultSet
String getTableName (int i)
àmendapatkan table name
String getColumnName (int i)
à get the column names to i
String getColumnTypeName (int i)
à get the column type to i
Steps to get the metadata ResultSet:
1. Make a Statement
Statement STT = con.createStatement ();
2. Get the ResultSet (query execution)
String sql = "Select * from nm_tbl";
ResultSet rs = stt.executeQuery (sql);
3. Get metadata
ResultSetMetaData RSMD = rs.getMetaData ();
4. Get number of columns
int j = rsmd.getColumnCount ();
5. Get table name
String nm_tbl = rsmd.getTableName (index);
6. Get field names and types, with a looping
for (int i = 1; i <= j; i + +)
String nm_field = rsmd.getColumnName (i);
String tp_field = rsmd.getColumnTypeName (i);
Examples
On an existing project, add a form with the draft as follows:

CONNECTION TO DATABASE, metadata

At The OK button, enter the following code

CONNECTION TO DATABASE, metadata

Run, will be obtained as follows:

CONNECTION TO DATABASE, metadata

 

end

0 comments:

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Post a Comment

thank you for visiting my blog and comment here. I hope the commentary contains a comment which intend to build and repair. criticism and suggestions I gladly accept.

Popular Posts

 

Total Pageviews

stat

© Copyright 2010. http://programminghaha.blogspot.com . All rights reserved | http://programminghaha.blogspot.com is proudly powered by Blogger.com | Template by o-om.com - zoomtemplate.com