Sie sind auf Seite 1von 10

Ex.No. 14. Create a Student database and store the details of the students in a table.

Perform the
5 SELECT, INSERT, UPDATE and DELETE operations using JDBC connectivity.

Part I: Database Creation:


  
Start Microsoft Office Access select Blank Database choose the directory you want to save your

10 database, give a name for your db click create button
In the Wizard, do the following:

15
1. Right click table1, choose Design view

20

25

30

35

2. Save the table, table name student.


40

45

50

3. Add Fields with specific datatypes


4. Save the student table and close.
5. Open the student table and enter at least one student details

10

15
Part II : Connecting database to ODBC [Steps for creating data source]

1. Click start Control Panel
2. Click Administrative Tools
3. Select Data Sources(ODBC)
20

25

30

35

40
4. Click Add button in the above wizard, choose the Driver option Microsoft Access
Driver(*.mdb, *.accdb) mentioned in the below picture.
5. Click finish, then data source wizard will open, in that type data source name: stud and
then click select button, select database wizard will open. Choose the directory and
locate the db file you created early. Then click ok
5

10

15

20

6. The created data source is now available in the data source wizard
25

30

35

Part III: Connect java program to ODBC


40
import java.sql.*; import
java.awt.*; import
java.awt.event.*; import
java.applet.*;
45

public class DBConF extends Frame implements ActionListener

50

{
TextField tn, trn,
55 tper; TextArea ta;
Button bsel, badd,
bmod; Connection con;
60
publicDBConF()
{
setLayout(new FlowLayout());
65
tn =new TextField(25);
trn =new TextField(10);
tper =new TextField(4); ta
= new TextArea(5, 25); bsel
= new Button("Select");
5 badd = new Button("Add");
bmod = new Button("Delete");
5 add(new Label("Name:"));
add(tn);
add(new Label("Roll
10
No.:")); add(trn);
add(new Label("Persentage:"));
add(tper);
add(bsel);
15

add(badd);

20

add(bmod);

25
add(ta);

30 bsel.addActionListener(this);
badd.addActionListener(this);

35
tper.addActionListener(this);

40 try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
45

con = DriverManager.getConnection("jdbc:odbc:stud","","");

50

}
catch(Exception e)
55
{

5
System.out.println(e);

10 }

addWindowListener(new WindowAdapter()
15

{
20 public void windowClosing(WindowEvent we)

{
25

setVisible(false);

30

System.exit(0);

35
}

40 });
}

45
public void actionPerformed(ActionEventae)

50 {
String ac = ae.getActionCommand();
if(ac.equals("Select"))
55 {
try
5 {
Statement st = con.createStatement();
ResultSetrs = st.executeQuery("select * from student");
10 ta.setText("");
while(rs.next())
15
{
ta.append("Name:\t" + rs.getString(1)+ "\n");
ta.append("Roll No:\t" + rs.getInt(2)+ "\n");
ta.append("Percentage:\t" + rs.getDouble(3)+ "\n");
ta.append("======================\n");
}
20

25 }
catch(Exception e)

30
{
System.out.println(e);
5

10

15
else if(ac.equals("Add"))

20 {

try
25

{
30 PreparedStatementpt = con.prepareStatement("insert into student

values(?, ?, ?)");
35

int t = Integer.parseInt(trn.getText());
40 double d = Double.parseDouble(tper.getText());
pt.setString(1, tn.getText());
pt.setInt(2, t);
pt.setDouble(3, d);
ta.setText("" + pt.executeUpdate() + "row inserted");
45

50 }
catch(Exception e)

5
{
System.out.println(e);

10

15
}

20 else

{
25

try
30 {
PreparedStatementpt = con.prepareStatement("delete from student where
RollNo=" + trn.getText());
35 ta.setText("" + pt.executeUpdate() + " row deleted");

}
40

catch(Exception e)
45 {

System.out.println(e);
50

}
}

10
public static void main(String ar[])

15 {
DBConFDf=new DBConF();
Df.setVisible(true);
Df.setTitle("JDB Connectivity");
20 Df.setSize(300,300);

25
}
}

Das könnte Ihnen auch gefallen