You are on page 1of 4

Page 1 of 4

subhashv1224@gmail.com

Open MySQL and perform the following as shown below:

1. now create a java form and name it as frmStudent as shown below :

tReg

tName

cStd cSection cMF tCity tDob

2. Click on the project name a. right click on the Libraries folder b. select the option Add Library from the shortcut menu c. From the dialog box select MYSQL JDBC DRIVER option d. Click on Add Library Button

Page 2 of 4
import java.sql.*; import javax.swing.*; public class student extends javax.swing.JFrame { Connection conn=null; Statement s; ResultSet rs; /** Creates new form student */ public student() { initComponents(); }

subhashv1224@gmail.com

private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) { try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); String url="jdbc:mysql://localhost/mydatabase"; String uname="root"; String pass="sv"; conn= DriverManager.getConnection(url, uname, pass); System.out.println("Success in Connectiion"); s=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); }catch(Exception e){System.out.println("Connection Failed");} } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void btnAllActionPerformed(java.awt.event.ActionEvent evt) { try{ rs=s.executeQuery("select * from student order by std,section,sname"); JOptionPane.showMessageDialog(this, "Connected...."); } catch(Exception e) { JOptionPane.showMessageDialog(this, "Not Able to Process Query"); System.out.println(e); } } private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { try{ rs=s.executeQuery("select * from student "); String stName=tName.getText(); stName=stName.toUpperCase(); rs.moveToInsertRow(); rs.updateString("regno", tReg.getText()); rs.updateString("std", ""+cStd.getSelectedItem()); rs.updateString("section", ""+cSection.getSelectedItem()); rs.updateString("mf", ""+cMF.getSelectedItem()); rs.updateString("sname",stName ); rs.updateString("city",tCity.getText() ); rs.updateString("dob",tDob.getText()); rs.insertRow(); System.out.println("Record Added Sucessfully"); } catch(Exception e) { System.out.println(e); tReg.requestFocus(); } }

Page 3 of 4

subhashv1224@gmail.com

private void btnFetchActionPerformed(java.awt.event.ActionEvent evt) { try{ rs=s.executeQuery("select * from student where regno= " + tReg.getText() + " order by std,section"); rs.first(); cStd.setSelectedItem(""+rs.getString("std")); cSection.setSelectedItem(""+rs.getString("section")); tName.setText(rs.getString("sname")); tReg.setText(rs.getString("regno")); cMF.setSelectedItem(""+rs.getString("mf")); tCity.setText(rs.getString("city")); tDob.setText(rs.getString("dob"));

} catch(Exception e) { JOptionPane.showMessageDialog(this, "No Such Reg #"); System.out.println(e); tReg.requestFocus(); } } private void btnChangeActionPerformed(java.awt.event.ActionEvent evt) { try{ String stName=tName.getText(); stName=stName.toUpperCase();

rs.updateString("regno", tReg.getText()); rs.updateString("std", ""+cStd.getSelectedItem()); rs.updateString("section", ""+cSection.getSelectedItem()); rs.updateString("mf", ""+cMF.getSelectedItem()); rs.updateString("sname",stName ); rs.updateString("city",tCity.getText() ); rs.updateString("dob",tDob.getText() );

rs.updateRow(); JOptionPane.showMessageDialog(this,"Record Updated Sucessfully"); } catch(Exception e) { System.out.println(e); tReg.requestFocus(); } } private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) { try{ int ans=JOptionPane.showConfirmDialog(this,"Are you sure to Delete"); if(ans==0){ rs.deleteRow(); JOptionPane.showMessageDialog(this,"Record Deleted Sucessfully"); } else{ JOptionPane.showMessageDialog(this,"Operation Aborted"); } } catch(Exception e) { System.out.println(e); tReg.requestFocus(); } } private void btnFActionPerformed(java.awt.event.ActionEvent evt) { try{ rs.first(); setValues(); }catch(Exception e){} } private void btNActionPerformed(java.awt.event.ActionEvent evt) { try{ rs.next(); setValues(); }catch(Exception e){ } }

Page 4 of 4

subhashv1224@gmail.com

private void btnPActionPerformed(java.awt.event.ActionEvent evt) { try{ rs.previous(); setValues(); }catch(Exception e){ } } private void btnLActionPerformed(java.awt.event.ActionEvent evt) { try{ rs.last(); setValues(); }catch(Exception e){ } } private void btnExitActionPerformed(java.awt.event.ActionEvent evt) { this.dispose(); } public void setValues(){ try{ cStd.setSelectedItem(""+rs.getString("std")); cSection.setSelectedItem(""+rs.getString("section")); tName.setText(rs.getString("sname")); tReg.setText(rs.getString("regno")); cMF.setSelectedItem(""+rs.getString("mf")); tCity.setText(rs.getString("city")); tDob.setText(rs.getString("dob"));

} catch(Exception e) { try{ if(rs.isAfterLast()){ JOptionPane.showMessageDialog(this, "This is the Last Record"); rs.last(); } if(rs.isBeforeFirst()){ JOptionPane.showMessageDialog(this, "This is the First Record"); rs.first(); } } catch(Exception e1){ } // end inner catch tReg.requestFocus(); } //end outer catch } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new student().setVisible(true); } }); } // Variables declaration - do not modify
private javax.swing.JButton btN; private javax.swing.JButton btnAdd; private javax.swing.JButton btnAll; private javax.swing.JButton btnChange; private javax.swing.JButton btnClear; private javax.swing.JButton btnConnect; private javax.swing.JButton btnDelete; private javax.swing.JButton btnExit; private javax.swing.JButton btnF; private javax.swing.JButton btnFetch; private javax.swing.JButton btnL; private javax.swing.JButton btnP; private javax.swing.JComboBox cMF; private javax.swing.JComboBox cSection; private javax.swing.JComboBox cStd; private javax.swing.JTextField tCity; private javax.swing.JTextField tDob; private javax.swing.JTextField tName; private javax.swing.JTextField tReg;

// End of variables declaration }

You might also like