0% found this document useful (0 votes)
19 views14 pages

Mod5 10

Uploaded by

pp6524878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

Mod5 10

Uploaded by

pp6524878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Jdbc programs

Create a mysql table employee (empcode int


primary key, empname
varchar(10),designation varchar(10)). Write a
Java program to read employee details from
user and insert it in to the table.
Create database for eg: rose
Create table employee with specified fields.
(set empcode as primary key and auto
increment)
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
public class Record {
public static void main(String[] args){
JFrame f=new JFrame();
JLabel label1=new JLabel("Employee Name:
");
JLabel label2=new JLabel("Designation: ");
JTextField text1=new JTextField(20);
JTextField text2=new JTextField(20);
JButton b=new JButton("Save");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent
ae){
String v1=text1.getText();
String v2=text2.getText();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:
3306/rose", "root", "");
Statement stmt=con.createStatement();
int i=stmt.executeUpdate("insert into
employee(empname,designation)
values('"+v1+"','"+v2+"')");
JOptionPane.showMessageDialog(null,"Data is
inserted successfully");
}
catch(Exception ex){
System.out.println(ex);
}
});
JPanel p=new JPanel(new GridLayout(3,2));
p.add(label1);
p.add(text1);
p.add(label2);
p.add(text2);
p.add(b);
f.add(p);
f.setVisible(true);
f.setSize(500,500);
}
}
Create a mysql table emp (empno varchar(10)
primary key, ename varchar(10),age int)
and insert five records.
Write a java program to read age from user
and display employees records whose age is
greater than user input.
Create database for eg: office
USE office

Create a table named emp with specified


fields
insert five records
import java.io.*;
import java.util.*;
import java.sql.*;
public class Emp2 {
public static void main(String[] args)throws
ClassNotFoundException {
Connection conn = null;
Statement stmt=null;
String url = "jdbc:mysql://localhost:3306/office";
Scanner sc=new Scanner(System.in);
System.out.println("Enter age of
employee:");
int a=sc.nextInt();
try
{
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(url,
"root”, “”);
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(“SELECT *
FROM emp WHERE age>a");
while(rs.next())
{
String s1=rs.getString(“empno");
String s2=rs.getString(“ename");
int s3=rs.getInt(“age");

System.out.println(“employee no, name and age


are :"+s1+"\t"+s2 +"\t"+s3);
}
}
catch (SQLException e) {
System.out.println(e);
}
}
}

You might also like