7.Data Insertion with SQL


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Inserted extends JFrame implements ActionListener
{

JLabel l3,l4,l5,l7;
JTextField t1,t2,t3,t4;
JButton b1;
public Inserted()
{
//———back—————-

l3=new JLabel(“Date :”);
l3.setBounds(20,80,80,30);
l3.setFont(new Font(“Freestyle Script”,Font.BOLD,25));
l3.setForeground(new Color(50,53,60));

l4=new JLabel(“Employee Name :”);
l4.setBounds(50,120,150,30);
l4.setFont(new Font(“Freestyle Script”,Font.BOLD,25));
l4.setForeground(new Color(50,53,60));

l5=new JLabel(“Contact no :”);
l5.setBounds(50,160,150,30);
l5.setFont(new Font(“Freestyle Script”,Font.BOLD,25));
l5.setForeground(new Color(50,53,60));

l7=new JLabel(“Email-ID :”);
l7.setBounds(50,330,150,30);
l7.setFont(new Font(“Freestyle Script”,Font.BOLD,25));
l7.setForeground(new Color(50,53,60));
//————–TextBox——————–
t1=new JTextField(10);
t1.setBounds(100,80,80,30);

t2=new JTextField(10);
t2.setBounds(200,120,150,30);

t3=new JTextField(10);
t3.setBounds(200,160,150,30);

t4=new JTextField(10);
t4.setBounds(200,330,150,30);
b1=new JButton(“Save”);
b1.setBounds(100,570,80,30);
b1.setFont(new Font(“Freestyle Script”,Font.BOLD,25));
b1.setForeground(new Color(10,133,10));
add(l3);add(t1);
add(l4);add(t2);
add(l5);add(t3);
add(l7);add(t4);
add(b1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

b1.addActionListener(this);

setLayout(null);
setSize(600,700);
setVisible(true);
setTitle(“Inserted”);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;

String deptno=t1.getText();
String dname=t2.getText();
String loc=t3.getText();
String salary=t4.getText();

try
{
String sql1=”insert into dept(deptno,dname,loc,salary) values(?,?,?,?)”;
Class.forName(“java.sql.Driver”);
conn= DriverManager.getConnection(“jdbc:mysql://localhost:3306/new?zeroDateTimeBehavior=convertToNull”,”root”,””);
stmt=conn.prepareStatement(sql1);

stmt.setString(1,deptno);
stmt.setString(2,dname);
stmt.setString(3,loc);
stmt.setString(4,salary);

stmt.executeUpdate();
System.out.println(“Record inserted Successfully”);
}catch (Exception e)
{
System.out.println(“error”+e);
}
}
}
public static void main(String args[])throws SQLException
{
new Inserted();
}
}