You are on page 1of 2

Suresh Rajpurohit <sureshpurohitt@gmail.

com>
1:41 PM (2 minutes ago)
to me

Step 2 Write the following code behind the save button:

Sy5t\m.out.pr1nt1n("Error ="+e);

) e1se

JOptionP�ne Sh0wMessageD1�a1og(nuH, "P1ease enter 1d" ):

try

int sid = Integer,parse1nt(1d.getText( )); String sname = name.getText( ):

String sfnamQ = fname.getText( ):

String saddrgss = address.getText( );

String scourge = course.getText( ): C1ass.f0rName("com.mysq1.jdbc.Driver");

con = (Connection)Dr1verManager.getConnect10n

(�jdbczmysq1://1oca1hostz3306/databasename"."username'.'Da$SW0rd'L Sta =
C0n.CPeateStatement( ): '

int 1 = Sta.executeUpdate("INSERT INTO


STUDENT(id.name.fname.address.course)VALUES("+sid-+",'�+sname +"', '"sfname+n~..

+saddress +"', "+course+-qn 1f(1>0) '

JOptionPane.showMessage01a1og(nu11,"Record saved successfu11y");

e1se JOptionPane.showMessageDialog(nu11."Record not saved successfu11y�);

resu1t.close( )f

sta.c1ose( ):

con.c1ose( );

}catch(Exception e)

{ }

System.out.pr1nt1n("Error: "+e);

Step 3 To Clear fields, write the following code behind the clear button:
1d.setText(""): name.setText("�); fname.setText(""); address.setText(""); course.
setText("");

Step 4 Write the following code behind the update button:

try
{

1.],

.1'f(1'>0)

C1ass.forName("com.mysq1.jdbc.Driver"): con=(Connect10n)DriverManager.getConnection
..

(" jdbc: mysql: //1oca1hostz3306/databasename";�username', "passwm%)v sta=con.


createStatement( ):

int 1=sta. executeUpdate("UPDATE STUDENT SET name='"+name. getText( )+". fname=" f


fname. getText() +"�, address ='�+address. getText( )-+"'. course.="+course.
getTextO+I

NHEREid=" +Integer. parseInt(1d. getText(H+

JOptionPane.showMessageDia1og(nu11. �Record updated successfu11y'): e1se

JOptionPane.showMessageDia1og(nu11."Record not updated successfully�):


resu1t.C]Ose( ): sta,c1ose( ); con,C]OSE( );

lcatch(EXC6pt10n e)
Step 5 F0! deletion o . ". . Peratlon, write the follow� � . 1�� 1d -QetText( )
-1ength()>0) mg code behmd the delete button.

try {

CIass.f0rNameg'com.mysq1.jdbc.0river'): con=(Connection)Driv

erManager.getConnection('jdbczmysqI://10ca1host:3306

' ldatabasename','username','password'); ,�ta f con.createStatement(); Int 1 =


sta.executeUpdate('DELETE FROM STUDENT WHERE 1d . ='+Integer.parse1nt(id.getText())
+"): 1f(i>0)

1 JOptionPane.showMessageDialogmul1, �Record de1eted successfu11y'); e se

JOptionPane.showMessageDialog(nul1, "Record not de1eted successfully�):


lcatch(Exception e)

l System.out.print1n(�Error :'+e); } . } else

JOptionPane.showMessageDia1og(nu11. "Please enter id");

{ }

System-Out.pr1nt1n("Error : �+e):