Monday, April 16, 2007

BookSale

import java.text.*;
import javax.swing.text.MaskFormatter;
import java.text.Format.*;
import java.text.ParseException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.*;
import java.util.*;
import java.util.Calendar;
import java.text.DateFormatSymbols;
import java.sql.*;



//CLASS OF frame 2 FOR STUDENT FILE

class BookSale extends JFrame{
//ds
String output="";
private Container pane = getContentPane();
private JTable table;
private Connection con;
int row=300,col=3;
private JLabel pic2, label, label1, label2, label3, label4,label5,label6,label7,label8,label9;
private JTextField field1, field2, field3, field4, field5, field6, field7,field8;
//JComboBox combo1, combo2, combo3;
//private String gender[] = {"Male","Female"};
//private String semester[] = {"First", "Second", "Summer"};
//private String status[] = {"New", "Old/Active", "Returnee", "Transferee"};


public BookSale()
{
setTitle("BOOK SALES FORM");

JTextArea textarea;

pane.setBackground(Color.black);
pane.setLayout(null);
Font font = new Font("Teletype",Font.PLAIN,18);


JLabel label = new JLabel("COR JESU COLLEGE\n Digos City",JLabel.CENTER);
label.setFont(font);
label.setBounds(50,10,400,40);
label.setForeground(Color.pink);
label.setBackground(Color.yellow);
label.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
pane.add(label,BorderLayout.CENTER);


//for pictures or logo
Icon logo = new ImageIcon("logo3.gif");
pic2 = new JLabel(logo);
pic2.setBounds(50,0,60,60);
pane.add(pic2);

//SET DATE HERE
textarea = new JTextArea();
GregorianCalendar today = new GregorianCalendar();

String[]weekdays = new DateFormatSymbols().getWeekdays();
String[]months = new DateFormatSymbols().getMonths();

output+=""+weekdays[today.get(today.DAY_OF_WEEK)];
output+=", "+(today.get(today.DATE))+" ";
output+=months[today.get(today.MONTH)]+"";
output+=today.get(today.YEAR)+"\n";

//LABELS HERE
label = new JLabel("B O O K S A L E F O R M");
label.setBounds(100,60,350,50);
label.setFont(new Font("Broadway BT",Font.BOLD,18));
label.setForeground(Color.yellow);
label.setBackground(Color.blue);
pane.add(label);


label1 = new JLabel("Date:");
label1.setBounds(280,120,200,20);
label1.setFont(new Font("Tahoma",Font.PLAIN,14));
label1.setForeground(Color.white);
pane.add(label1);


textarea.setFont(new Font("Tahoma",Font.PLAIN,14));
textarea.setEditable(false);
textarea.setText(output);
textarea.setBounds(320,120,180,20);
textarea.setForeground(Color.pink);
textarea.setBackground(Color.black);
pane.add(textarea);


label3 = new JLabel("Name:");
label3.setBounds(10,120,250,20);
label3.setFont(new Font("Tahoma",Font.PLAIN,14));
label3.setForeground(Color.white);
pane.add(label3);
field3 = new JTextField();
field3.setBounds(50,120,200,20);
field3.setText("");
field3.setFont(new Font("Tahoma",Font.PLAIN,16));
field3.setForeground(Color.blue);
pane.add(field3);
//pane.add(table);



//FOR TABLE
//JTable inventTable = new JTable(row,col);
// inventTable.setGridColor(Color.gray);

//JScrollPane inventScroll = new JScrollPane(inventTable);
//inventScroll.setBounds(20,150,450,100);
//pane.add(inventScroll);


label4 = new JLabel("TOTAL P:");
label4.setBounds(300,270,150,20);
label4.setFont(new Font("Tahoma",Font.PLAIN,14));
label4.setForeground(Color.white);
pane.add(label4);
field4 = new JTextField();
field4.setBounds(360,270,120,20);
field4.setText("");
field4.setFont(new Font("Tahoma",Font.PLAIN,16));
field4.setForeground(Color.blue);
pane.add(field4);


label5 = new JLabel("Receive Payment By:");
label5.setBounds(150,310,150,20);
label5.setFont(new Font("Tahoma",Font.PLAIN,14));
label5.setForeground(Color.white);
pane.add(label5);
field5 = new JTextField();
field5.setBounds(290,310,190,20);
field5.setText("");
field5.setFont(new Font("Tahoma",Font.PLAIN,16));
field5.setForeground(Color.blue);
pane.add(field5);


label6 = new JLabel("Cashier");
label6.setBounds(360,330,250,20);
label6.setFont(new Font("Tahoma",Font.PLAIN,14));
label6.setForeground(Color.white);
pane.add(label6);

label7 = new JLabel("O.R. Number:");
label7.setBounds(290,360,150,20);
label7.setFont(new Font("Tahoma",Font.PLAIN,14));
label7.setForeground(Color.white);
pane.add(label7);
field7 = new JTextField();
field7.setBounds(375,360,105,20);
field7.setText("");
field7.setFont(new Font("Tahoma",Font.PLAIN,16));
field7.setForeground(Color.blue);
pane.add(field7);

label8 = new JLabel("Recieved in Good Condition By:");
label8.setBounds(120,390,250,20);
label8.setFont(new Font("Tahoma",Font.PLAIN,14));
label8.setForeground(Color.white);
pane.add(label8);
field8 = new JTextField();
field8.setBounds(290,390,190,20);
field8.setText("");
field8.setFont(new Font("Tahoma",Font.PLAIN,16));
field8.setForeground(Color.blue);
pane.add(field8);

label9 = new JLabel("Parent/Guardian/Student");
label9.setBounds(300,410,250,20);
label9.setFont(new Font("Tahoma",Font.PLAIN,14));
label9.setForeground(Color.white);
pane.add(label9);

JPanel bottompanel=new JPanel();
bottompanel.setBackground(Color.white);

JButton buttonnew;
JButton buttonsave;
JButton buttonsearch;
JButton buttonprint;
JButton buttonclose;
JButton buttondelete;


//dimension size of button
Dimension size=new Dimension(50,30);
//bevel JLabel label1 = newbutton
Border edge=BorderFactory.createRaisedBevelBorder();

//declare bottons



buttonnew = new JButton("NEW");
buttonnew.setBounds(10,460,60,30);
buttonnew.setPreferredSize(size);
buttonnew.setBorder(edge);
buttonnew.setToolTipText("click this button to add file.");
buttonnew.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
buttonnew.setFont(new Font("Arial",Font.BOLD,14));
buttonnew.setBackground(new Color(10,250,210));
buttonnew.setForeground(new Color(0,5,255));
buttonnew.setMnemonic('N');
bottompanel.add(buttonnew);

buttonsave = new JButton("SAVE");
buttonsave.setPreferredSize(size);
buttonsave.setBorder(edge);
buttonsave.setToolTipText("it saves the files");
buttonsave.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
buttonsave.setFont(new Font("Arial",Font.BOLD,14));
buttonsave.setForeground(new Color(0,5,255));
buttonsave.setBackground(new Color(10,250,210));
buttonsave.setBounds(100,460,60,30);
buttonsave.setMnemonic('S');
bottompanel.add(buttonsave);

buttonprint = new JButton("PRINT");
buttonprint.setBounds(190,460,60,30);
buttonprint.setPreferredSize(size);
buttonprint.setBorder(edge);
buttonprint.setToolTipText("it gives you a hard copy");
buttonprint.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
buttonprint.setFont(new Font("Arial",Font.BOLD,14));
buttonprint.setForeground(new Color(0,5,255));
buttonprint.setBackground(new Color(10,250,210));
buttonprint.setMnemonic('P');
bottompanel.add(buttonprint);

buttonsearch = new JButton("SEARCH");
buttonsearch.setBounds(280,460,60,30);
buttonsearch.setPreferredSize(size);
buttonsearch.setBorder(edge);
buttonsearch.setToolTipText("click this button to find");
buttonsearch.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
buttonsearch.setFont(new Font("Arial",Font.BOLD,14));
buttonsearch.setForeground(new Color(0,5,255));
buttonsearch.setBackground(new Color(10,250,210));
buttonsearch.setMnemonic('R');
bottompanel.add(buttonsearch);

buttondelete = new JButton("DELETE");
buttondelete.setBounds(370,460,60,30);
buttondelete.setPreferredSize(size);
buttondelete.setBorder(edge);
buttondelete.setToolTipText("Click this button to erase the file"); buttondelete.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
buttondelete.setFont(new Font("Arial",Font.BOLD,14));
buttondelete.setForeground(new Color(0,5,255));
buttondelete.setBackground(new Color(10,250,210));
buttondelete.setMnemonic('D');
bottompanel.add(buttondelete);


buttonclose = new JButton("CLOSE");
buttonclose.setBounds(460,460,60,30);
buttonclose.setPreferredSize(size);
buttonclose.setBorder(edge);
buttonclose.setToolTipText("Click this button to terminate the frame"); buttonclose.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
buttonclose.setFont(new Font("Arial",Font.BOLD,14));
buttonclose.setForeground(new Color(0,5,255));
buttonclose.setBackground(new Color(10,250,210));
buttonclose.setMnemonic('C');
bottompanel.add(buttonclose);

//pane.add(bottompanel);
pane.add(buttonsearch);
pane.add(buttonnew);
pane.add(buttonprint);
pane.add(buttonsave);
pane.add(buttonclose);
pane.add(buttondelete);


buttonclose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setVisible(false);
}
}
);






//display table



String url = "jdbc:odbc:cim";
// pane.setLayout(null);

// Load the driver to allow connection to the database
try {
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

con = DriverManager.getConnection(
url );
}
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"Failed to load JDBC/ODBC driver." );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
catch ( SQLException sqlex ) {
System.err.println( "Unable to connect" );
sqlex.printStackTrace();
}

getTable();

setSize( 450, 150 );
show();
}

private void getTable()
{
Statement statement;
ResultSet resultSet;

try {
String query = "SELECT * FROM Table1";

statement = con.createStatement();
resultSet = statement.executeQuery( query );
displayResultSet( resultSet );
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}

private void displayResultSet( ResultSet rs )
throws SQLException
{
// position to first record
boolean moreRecords = rs.next();

// If there are no records, display a message
if ( ! moreRecords ) {
JOptionPane.showMessageDialog( this,
"ResultSet contained no records" );
setTitle( "No records to display" );
return;
}

setTitle( "Authors table from Books" );

Vector columnHeads = new Vector();
Vector rows = new Vector();

try {
// get column heads
ResultSetMetaData rsmd = rs.getMetaData();

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );

// get row data
do {
rows.addElement( getNextRow( rs, rsmd ) );
} while ( rs.next() );

// display table with ResultSet contents
table = new JTable(rows, columnHeads);
JScrollPane scroller = new JScrollPane( table );
scroller.setBounds(50,150,430,100);
pane.add(scroller);
//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 <= rsmd.getColumnCount(); ++i )
switch( rsmd.getColumnType( i ) ) {
case Types.VARCHAR:
currentRow.addElement( rs.getString( i ) );
break;
case Types.INTEGER:
currentRow.addElement(
new Long( rs.getLong( i ) ) );
break;
default:
System.out.println( "Type was: " +
rsmd.getColumnTypeName( i ) );
}

return currentRow;
}
}

No comments: