iishiii
November 24th, 2009, 07:23 AM
i have to develop a simple GUI based database driven application for Bank system.
and required to use MS Access to create a database named Bank and add a table named Account into this database.
The Account table must have following fields:
1. Account Number
2. Account Holder Name
3. Account Type
4. Account Balance
You must enter 10 records within Account table of Bank database.
Create a system DSN (Data Source Name) named bankDSN and use this DSN in a program to connect to this database.
Write a program which will have following functionalities:
1) A method which will have the functionality to connect to database.
2) A method which will retrieve all the records of user from database and display on GUI window. The top header of GUI window must have columns name of table Account. The column names displayed on GUI must be same as columns name within table of database. You have to use ResultSetMetaData object for this purpose. You cannot use hard coded values to display columns name.
The above method will connect to this database and query all the records from the “Account” table using SQL statement and display them on GUI application.
You can use Grid Layout to display the records from database to GUI application.
You must use ResultSetMetaData to display exact name of columns of database table on the top of GUI application. Marks will be deducted in case of not using ResultSetMetaData.
i have written the code but it have many errors and i cant figure them out because i have very short time
here am posting my code please help me out in this regard i will be very helpful to you peoples
package bank;
public class Main {
public static void main(String[] args) {
}
}
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Bank = new javax.swing.JLabel();
jTextField = new javax.swing.JTextField();
button = new java.awt.Button();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Bank.setFont(new java.awt.Font("Palatino Linotype", 3, 12)); // NOI18N
Bank.setText("Bank");
button.setLabel("Click Me To Show Accounts Detail");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(178, 178, 178)
.addComponent(Bank, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(jTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(98, 98, 98)
.addComponent(button, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(41, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Bank, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(99, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
import java.sql.*;
public class MetaDataEx {
public static void main (String args[ ]) {
try {
//Step 2: load driver
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
//Step 3: define the connection URL
String url = “jdbc:odbc:BankDSN”;
//Step 4: establish the connection
Connection con = null;
con = DriverManager.getConnection(url, “”, “”);
//Step 5: create PrepareStatement by passing sql and
// ResultSet appropriate fields
String sql = “SELECT * FROM Bank”;
PreparedStatement pStmt = con.prepateStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//Step 6: execute the query
ResultSet rs = pStmt.executeQuery();
// get ResultSetMetaData object from rs
ResultSetMetaData rsmd = rs.getMetaData( );
// printing no. of column contained by rs
int numColumns = rsmd.getColumnCount();
System.out.println(“Number of Columns:” + numColumns);
// printing all column names by using for loop
String cName;
for(int i=1; i<= numColumns; i++) {
cName = rsmd.getColumnName(i);
System.out.println(cName);
System.out.println(“\t”);
}
// changing line or printing an empty string
System.out.println(“ ”);
// printing all values of ResultSet by iterating over it
String id, name, add, ph;
while( rs.next() )
{
CustomerName = rs.getString(1);
AccountNo = rs.getString(2);
AccountType = rs.getString(3);
DateOpened = rs.getString(4);
Balance = rs.getString(5);
System.out.println(CustomerName);
System.out.println(“\t”);
System.out.println(AccountNo);
System.out.println(“\t”);
System.out.println(AccountType);
System.out.println(“\t”);
System.out.println(DateOpened);
System.out.println(“\t”);
System.out.println(Balance);
System.out.println(“ ”);
}
//Step 8: close the connection
con.close();
}catch(Exception sqlEx){
System.out.println(sqlEx);
}
} // end main
} // end class
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Bank;
private java.awt.Button button;
private javax.swing.JTextField jTextField;
// End of variables declaration
}
and required to use MS Access to create a database named Bank and add a table named Account into this database.
The Account table must have following fields:
1. Account Number
2. Account Holder Name
3. Account Type
4. Account Balance
You must enter 10 records within Account table of Bank database.
Create a system DSN (Data Source Name) named bankDSN and use this DSN in a program to connect to this database.
Write a program which will have following functionalities:
1) A method which will have the functionality to connect to database.
2) A method which will retrieve all the records of user from database and display on GUI window. The top header of GUI window must have columns name of table Account. The column names displayed on GUI must be same as columns name within table of database. You have to use ResultSetMetaData object for this purpose. You cannot use hard coded values to display columns name.
The above method will connect to this database and query all the records from the “Account” table using SQL statement and display them on GUI application.
You can use Grid Layout to display the records from database to GUI application.
You must use ResultSetMetaData to display exact name of columns of database table on the top of GUI application. Marks will be deducted in case of not using ResultSetMetaData.
i have written the code but it have many errors and i cant figure them out because i have very short time
here am posting my code please help me out in this regard i will be very helpful to you peoples
package bank;
public class Main {
public static void main(String[] args) {
}
}
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Bank = new javax.swing.JLabel();
jTextField = new javax.swing.JTextField();
button = new java.awt.Button();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Bank.setFont(new java.awt.Font("Palatino Linotype", 3, 12)); // NOI18N
Bank.setText("Bank");
button.setLabel("Click Me To Show Accounts Detail");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(178, 178, 178)
.addComponent(Bank, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(jTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(98, 98, 98)
.addComponent(button, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(41, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(Bank, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(99, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
import java.sql.*;
public class MetaDataEx {
public static void main (String args[ ]) {
try {
//Step 2: load driver
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
//Step 3: define the connection URL
String url = “jdbc:odbc:BankDSN”;
//Step 4: establish the connection
Connection con = null;
con = DriverManager.getConnection(url, “”, “”);
//Step 5: create PrepareStatement by passing sql and
// ResultSet appropriate fields
String sql = “SELECT * FROM Bank”;
PreparedStatement pStmt = con.prepateStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//Step 6: execute the query
ResultSet rs = pStmt.executeQuery();
// get ResultSetMetaData object from rs
ResultSetMetaData rsmd = rs.getMetaData( );
// printing no. of column contained by rs
int numColumns = rsmd.getColumnCount();
System.out.println(“Number of Columns:” + numColumns);
// printing all column names by using for loop
String cName;
for(int i=1; i<= numColumns; i++) {
cName = rsmd.getColumnName(i);
System.out.println(cName);
System.out.println(“\t”);
}
// changing line or printing an empty string
System.out.println(“ ”);
// printing all values of ResultSet by iterating over it
String id, name, add, ph;
while( rs.next() )
{
CustomerName = rs.getString(1);
AccountNo = rs.getString(2);
AccountType = rs.getString(3);
DateOpened = rs.getString(4);
Balance = rs.getString(5);
System.out.println(CustomerName);
System.out.println(“\t”);
System.out.println(AccountNo);
System.out.println(“\t”);
System.out.println(AccountType);
System.out.println(“\t”);
System.out.println(DateOpened);
System.out.println(“\t”);
System.out.println(Balance);
System.out.println(“ ”);
}
//Step 8: close the connection
con.close();
}catch(Exception sqlEx){
System.out.println(sqlEx);
}
} // end main
} // end class
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Bank;
private java.awt.Button button;
private javax.swing.JTextField jTextField;
// End of variables declaration
}