AJP Practical 18
AJP Practical 18
AJP Practical 18
Program code 1: Write a program to create a student table in database and insert a record in a student table.
import java.sql.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
public class JDBCProgram5 implements ActionListener{
Frame f1;
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1;
static Statement stmt;
JDBCProgram5() {
f1=new Frame();
l1=new Label("Roll NO:");
l2=new Label("Name:");
l3=new Label("Marks:"); Output :-
t1=new TextField(20);
t2=new TextField(20);
t3=new TextField(20);
b1=new Button("Insert");
f1.add(l1);
f1.add(t1);
f1.add(l2);
f1.add(t2);
f1.add(l3);
f1.add(t3);
f1.add(b1);
b1.addActionListener(this);
f1.setLayout(new FlowLayout());
f1.setSize(500,500);
f1.setVisible(true);
}
public static void main(String[] args) throws SQLException {
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata","root"
,"root");
stmt=con.createStatement();
new JDBCProgram5();
}
@Override
public void actionPerformed(ActionEvent arg0) {
String query="insert into
stud(rollno,name,marks)values('"+t1.getText()+"','"+t2.getText()+"','"+t3.getText(
)+"');";
try {
int cn=stmt.executeUpdate(query);
if(cn>0) {
JOptionPane.showMessageDialog(f1, "Row inserted");
}else {
JOptionPane.showMessageDialog(f1, "cannot insert a row");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Vedika Mohite
Practical 18 :Write a program to insert and retrieve data from database using JDBC– 2200100176 Vedika Mohite
import java.sql.*;
public class JDBCProgram8 {
Output :-
Vedika Mohite
Practical 18 :Write a program to insert and retrieve data from database using JDBC– 2200100176 Vedika Mohite
Exercise Q.1: Write a program to create employee table in database having two columns “emp_id” and “emp_name”.
import java.sql.*;
public class JDBCProgram6 {
Output :-
Vedika Mohite
Practical 18 :Write a program to insert and retrieve data from database using JDBC– 2200100176 Vedika Mohite
Exercise Q.2: Develop a program to display the name and roll_no of students from “student table”having
percentage>70.
import java.sql.*;
public class JDBCProgram7 {
Output :-
Vedika Mohite