Java Programming – Accessing Databases with JDBC You can control access to databases with Java. The feature of Java for this purpose Java Database Connectivity (JDBC). You have to create a database in MS Access or Oracle or MySQL or another database management system. Then enter a name for your ODBC database. Visit http://learnjavatoday. blogspot. com For more details / / The following example shows how to display in a table / / Using JAVA - Java Database Connectivity (JDBC). / / Create before running the program, a table / / How books named in Oracle 9i. / / Declare the ODBC database type name bk. / / User name is kt. / / Password is kt. / / Save the file as MyTable. Java / / Assuming that your Java bin directory C: javabin / / Compile the file with / / C: javabin javac MyTable. Java / To create / MyTable. class file. / / Run the program with / / C: javabin java MyTable. import java. SQL. *; Import javax. Swing. *; import java. AWT. *; import java. AWT. Event. *; import java. util. *; public class MyTable extends JFrame ( private Connection connection; private JTable table; public MyTable () ( String url = “jdbc: odbc: bk”; String username = “KT”; String password = “KT”; try ( Class. Class.forName (“So JDBC. ODBC. JdbcOdbcDriver”); Connection = DriverManager. getConnection (URL, user name, password); ) catch (ClassNotFoundException cnfex) ( System. err. to load println (“Failed to JDBC / ODBC driver.”); cnfex. printStackTrace (); System. exit (1); ) catch (SQLException sqlex) ( System. err. Println (“Unable to connect”); sqlex. printStackTrace (); ) getTable (); setSize (450,150); show (); ) private void getTable () ( Statement; ResultSet ResultSet; try ( String query = “SELECT * FROM BOOKS”; statement = connection. createStatement (); resultSet = statement. executeQuery (query); displayResultSet (ResultSet); Statement. close (); ) catch (SQLException sqlex) ( sqlex. printStackTrace (); ) ) private void displayResultSet (ResultSet rs) throws SQLException ( boolean moreRecords = rs. next (); if (! moreRecords) ( JOptionPane. showMessageDialog (this, “ResultSet contained no records”); setTitle (“No records are displayed”); Return; ) setTitle (“Authors Table of Books”); ColumnHeader Vector = new Vector (); Vector lines = new Vector (); try ( ResultSetMetaData RSMD = rs. getMetaData (); for (int i = 1, i ColumnHeader. addElement (rsmd. getColumnName (i)); do ( Lines. addElement (getNextRow (rs, RSMD)); ) While (case next ()); Table = new JTable (rows, ColumnHeader); JScrollPane scroller = new JScrollPane (table); getContentPane (). add (scroller, BorderLayout. Center); validate (); ) catch (SQLException sqlex) ( sqlex. printStackTrace (); ) ) private Vector getNextRow (ResultSet rs, ResultSetMetaData RSMD) throws SQLException ( Vector currentRow = new Vector (); for (int i = 1, i Switch (rsmd. getColumnType (i)) ( When types. VARCHAR: currentRow. addElement (case getString (i)); break; When types. DECIMAL: currentRow. addElement (new Long (getLong Case (i))); break; Standard: System. out. Println (“Give It read:” + RSMD. GetColumnTypeName (i)); ) Return currentRow; ) public void shutdown () ( try ( Connection. close (); ) catch (SQLException sqlex) ( System. err. Can println (“do not disconnect”); sqlex. printStackTrace (); ) ) public static void main (String args []) ( final app = new MyTable MyTable (); App. addWindowListener ( new WindowAdapter () ( public void windowClosing (WindowEvent e) ( App. (Shutdown); System. exit (0); ) ) ); ) ) / / For more details see How To Java program
