5 Replies - 1071 Views - Last Post: 26 November 2009 - 12:46 AM Rate Topic: -----

#1 iishiii   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-November 09

Code Help

Post icon  Posted 24 November 2009 - 06:06 AM

I am 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

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 done according to my Knowledge but i dont know there are many errors in my code and i have very short time please here am giving me written code to correct it please help me out


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

}
 


Is This A Good Question/Topic? 0
  • +

Replies To: Code Help

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12315
  • View blog
  • Posts: 45,414
  • Joined: 27-December 08

Re: Code Help

Posted 24 November 2009 - 09:33 AM

It is really bad practice to enclose a class within an ActionListener. ActionListeners are designed to be efficient and lightweight, and by adding a class within it, you are almost negating this entirely. Also, import statements go at the top of the file before you start working with the outer class, not right before the inner statement.

Before I forget, I bolded and underlined this for emphasis, so it is important. Don't ever, ever, ever, EVER use a GUI Builder. The code is terrible. Build your own. If you need any more help with your code, please properly [b][u]indent and space everything in your class. It is a headache to read the database part of your code.
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Code Help

Posted 24 November 2009 - 06:16 PM

Your going too fast and try to make to much in one shot

Start by the GUI fix things like that

Bank = new javax.swing.JLabel();
jTextField = new javax.swing.JTextField();
button = new java.awt.Button();

that should be at least

JLabel Bank = new javax.swing.JLabel();
JTextFiled jTextField = new javax.swing.JTextField();
JButton button = new java.awt.Button();

keep your GUI and your Database processing as separate as possible
Your GUI should NOT know if you are talking to a Database, flat files, or TCP connection to get the data

And get ride of the GUI generator you will never learn that way
Was This Post Helpful? 0
  • +
  • -

#4 iishiii   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-November 09

Re: Code Help

Posted 25 November 2009 - 05:16 AM

View Postpbl, on 24 Nov, 2009 - 05:16 PM, said:

Your going too fast and try to make to much in one shot

Start by the GUI fix things like that

Bank = new javax.swing.JLabel();
jTextField = new javax.swing.JTextField();
button = new java.awt.Button();

that should be at least

JLabel Bank = new javax.swing.JLabel();
JTextFiled jTextField = new javax.swing.JTextField();
JButton button = new java.awt.Button();

keep your GUI and your Database processing as separate as possible
Your GUI should NOT know if you are talking to a Database, flat files, or TCP connection to get the data

And get ride of the GUI generator you will never learn that way




i have done in other way but it also having error not going to be compiled
i hope some one can find error in this code for me

i will be very thankful to him
// importing packages

	import java.awt.*;
	import java.io.*;
	import javax.swing.*;
	import java.sql.*;
				// defining class BankSystem

	public class BankSystem{
	private JFrame frame;
	private Container cont;
	private Connection dbCon;
	private Statement st;
	private ResultSet resultSet;
	private ResultSetMetaData rsmd;

		// constructor

	public BankSystem(){
		connection();
		displayData(); // Showing Accout Table Data on Window
	}
		// method that retrieving data from database & showing on GUI

	public void displayData(){
	frame = new JFrame();
	cont = frame.getContentPane();
try{

	String sql = "SELECT * FROM Account"; 		// SQL select statement 
	res0ultSet = st.executeQuery(sql);		// execute the SQL statement
	rsmd = resultSet.getMetaData(); 
	int columns = rsmd.getColumnCount(); 
	resultSet.last(); 
	int rows = resultSet.getRow() + 1; // returns the index of last row and adding 1
	cont.setLayout(new GridLayout(rows,columns)); 
	for (int i =1; i<= columns; i++)
	{


							// Adding columns names to Frame

	cont.add(new JLabel(rsmd.getColumnName(i)));
	}
	resultSet.first();
	while (resultSet.next()){ 
	JLabel label1 = new JLabel(resultSet.getString(1)); 	// Setting field value
	JLabel label2 = new JLabel(resultSet.getString(2)); 	// Setting field value 
	JLabel label3 = new JLabel(resultSet.getString(3)); 	// Setting field value
	JLabel label4 = new JLabel("" + resultSet.getInt(4)); 	// Setting field value 
	cont.add(label1); 				// Adding label1 
	cont.add(label2); 				// Adding label2
	cont.add(label3); 				// Adding label3 
	cont.add(label4); 				// Adding label4
	}
	dbCon.close(); 
	}catch(Exception sqlEx){ 
	System.out.println(sqlEx); 
	}
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	frame.setSize(400,500); 		// Size of the Window
	frame.setVisible(true); 		// Displaying Window On Screen
	frame.setResizable(false); 		// Stoping Window from resizing
	}

					// method that create a connection to database
		
	public void connection()
	{

try{
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	String url = "jdbc:odbc:bankDSN";
	dbCon = DriverManager.getConnection(url); 		// COnnection establish
	st = dbCon.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 					ResultSet.CONCUR_UPDATABLE);

	}catch(Exception sqlEx){ 
	System.out.println(sqlEx); 				// exception print on screen

	}
	} 							// end of connection method

	public static void main(String args[]){
	BankSystem a = new BankSystem();
	}							// main end

	} 							// class end
	

Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Code Help

Posted 25 November 2009 - 04:32 PM

res0ultSet = st.executeQuery(sql);

res0ultSet is not defined
Was This Post Helpful? 0
  • +
  • -

#6 iishiii   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-November 09

Re: Code Help

Posted 26 November 2009 - 12:46 AM

I have Done it

Thank You Very Much Mr Java Lover

Thank You
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1