pract18_s
pract18_s
Program Code:
1.Write a program to create a Student table in database and then insert a record in 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:");
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();
}
Maitreyee Lohar TYCO 2254
Output:
Output:
Maitreyee Lohar TYCO 2254
>Exercise:
1. Develop a program to create employee table in database having two columns “emp_id”
and “emp_name”.
Output:
Maitreyee Lohar TYCO 2254
2. Develop a program to display name and roll_no of students from “student table”
having percentage>70.