Insert data into database using JDBC

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class adddata extends JFrame implements ActionListener
{
JButton btnSave;
adddata()
{
setSize(500,500);

btnSave=new JButton(“save data”);
btnSave.setBounds(100,100,100,25);
btnSave.addActionListener(this);
Container c=getContentPane();
c.setLayout(null);
c.add(btnSave);

setVisible(true);
}
public void actionPerformed(ActionEvent obj)
{
Connection con;
PreparedStatement ps;
ResultSet rs;
String database=”bca6.mdb”;
String url=”jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=” + database + “;”;

if(obj.getSource()==btnSave)
{
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
con=DriverManager.getConnection(url);
ps=con.prepareStatement(“Insert into stu (rollno, sname, result) values(?,?,?)”);
String r,n,re;
r=”111″;
n=”Shamsher singh”;
re=”fail”;
ps.setString(1,r);
ps.setString(2,n);
ps.setString(3,re);

int i=ps.executeUpdate();
setTitle(i + ” records inserted”);
}
catch(Exception e)
{}

}
}
}
class insert
{
public static void main(String ar[])
{
adddata a1=new adddata();
}
}

Author: Vikas Kansal

Hi.. I am Vikas Kansal. I am a "Computer Faulty" and a "Software & Web developer". I am an active member of A Social and Educational Organization NIRMAN.

Leave a Reply

Your email address will not be published. Required fields are marked *