
how to store data into database using JDBC & SQL

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class InsertData{
public static void main(String[] args){
JFrame f=new JFrame();
f.setLayout(null);
JLabel lab1=new JLabel("Name : ");
JLabel lab2=new JLabel("Address: ");
final JTextField text=new JTextField(20);
final JTextArea area=new JTextArea(5,20);
JScrollPane pane=new JScrollPane(area);
JButton b=new JButton("Insert");
lab1.setBounds(10,10,100,20);
text.setBounds(120,10,150,20);
lab2.setBounds(10,40,100,20);
area.setBounds(120,40,150,60);
b.setBounds(120,110,100,20);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String st1=text.getText();
String st2=area.getText();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
PreparedStatement pstmt = conn.prepareStatement("insert into data(name,address) values(?,?)");
pstmt.setString(1, st1);
pstmt.setString(2,st2 );
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null,"Inserted Successfully!");
}
catch(Exception ex){}
}
});
f.add(lab1);
f.add(text);
f.add(lab2);
f.add(area);
f.add(b);
f.setVisible(true);
f.setSize(350,200);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.