Skip to main content

Posts

Showing posts with the label jdbc

JDBC - Getting tables from database

You can retrieve all tables from database using JDBC. DatabaseMetaData object contains various method to get the meta information from any database. This object can be accessed by invoking getMetaData() on the Connection object. DatabaseMetaData dbmd=con.getMetaData();   After getting the DatabaseMetaData, you can use it to retrieve the tables from any database like following statement, ResultSet rs=dbmd.getTables(null,null,null,new String[]{"table"}); Here, you will get the ResultSet Object containing various other information like Schema, catalog, table name, etc. Table name available in third column of this ResultSet so we can get it as rs.getString(3); To get all tables from a database using Connection, we need to traverse entire ResultSet object. We have to invoke rs.next() till it returns false and on each invocation of rs.next(), get the data from third column to get the name of the table. Here is a method that returns the array of table names from the datab...

List dirvers and connection-urls for connecting java program to databases

DB2 Driver : "com.ibm.db2.jcc.DB2Driver" URL: "jdbc:db2://localhost:5021/database","user","password" Apache Derby driver: "org.apache.derby.jdbc.EmbeddedDriver" URL: "jdbc:derby://localhost/databasename","user","password" FrontBase Driver : "jdbc.frontbase.FBDriver" URL : "jdbc:frontbase:/DerbyDB/AssetDB","user","password" OpenBase Driver : "com.openbase.jdbc.ObDriver" URL : "jdbc:openbase://localhost/databasename","user","password" MySQL Driver : "com.mysql.jdbc.Driver" URL: "jdbc:mysql://localhost:3306/databasename","user","password" H2Database Driver : "org.h2.Driver" URL: " jdbc:h2:tcp://localhost//data/databasename ","user","password" More info. PostgreSQL Driver : "org.postgresql.Driver" URL: "jdbc:postgresql://localhos...

Generating JTable from database table

package  process; import  javax.swing. * ; import  javax.swing.table. * ; import  java.sql. * ; /**  * This class create JTable from Database table.  * User program needs to specify database connection and corresponding atable name.  * @author Hemraj  */ public   class  TableToJTable {      //private String table;      private   Connection  con;      public   TableToJTable ( Connection  con ){          this .con = con;      }      /**      * This method return JTable object created from Database table having same data asn structure      *...

Handling Transaction JDBC

Transactions   In various application we may group the series of statements in such a way that either all of them execute successfully or all to be failed. For example if we want to execute five SQL queries at a time, which are related to each other, like ü     Search the particular data ü      Again search for next from second row ü       add the data retrieved ü       Insert into third row ü       Update the row To do this in the single steps, we need to do the transaction in the given ordered. When multiple statements are executed in a single transaction, all operations can be committed (made permanent in database) or rollback (that is changes to the database are undone). When a new connection object is created, it is set to commit every time transaction automatically .so we can not rollback, to solve this problem we use the transaction scheme. There are the following ...