Complete Java Code
Complete Java Code
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost/bit","root","");
Statement std = (Statement) con.createStatement();
String qry = "INSERT INTO it6 VALUES ('" + txtid.getText() + "', '" +
txtname.getText() + "', '" + txtaddress.getText() + "')";
int x = std.executeUpdate(qry);
if (x==1)
{
JOptionPane.showMessageDialog(null, "insert suceesfully");
FillData();
}
else
{
JOptionPane.showMessageDialog(null, "inset Failled");
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, e);
}
}
}
When Clicked Fill Jtable
try
{
int row = jTable1.getSelectedRow();
String Table_Click=(jTable1.getModel().getValueAt(row, 0).toString());
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost/bit","root","");
Statement std = (Statement) con.createStatement();
String qry ="select * from it6 where ID='" + Table_Click+"'";
Statement pst = (Statement) con.prepareStatement(qry);
ResultSet rs;
rs=pst.executeQuery(qry);
if (rs.next())
{
String add1 = rs.getString("ID");
txtid.setText(add1);
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, e);
}
}
Update Code:
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost/bit","root","");
Statement std = (Statement) con.createStatement();
String qry = "UPDATE it6 SET Name = '" + txtname.getText() + "',
Address = '" + txtaddress.getText() + "' WHERE ID = '" + txtid.getText() +
"'";
int x = std.executeUpdate(qry);
if (x==1)
{
JOptionPane.showMessageDialog(null, "updated suceesfully");
FillData();
}
else
{
JOptionPane.showMessageDialog(null, "update Failled");
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, e);
}
}
Delete Code
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost/bit","root","");
Statement std = (Statement) con.createStatement();
String qry ="delete from it6 where ID='" + txtid.getText()+"'";
int x = std.executeUpdate(qry);
if (x==1)
{
JOptionPane.showMessageDialog(null, "Deleted suceesfully");
FillData();
}
else
{
JOptionPane.showMessageDialog(null, "Deleted Failled");
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, e);
}
}
Search Code:
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost/bit","root","");
String qry ="select * from it6 where ID='" + txtsearch.getText() +
"'" ;
Statement pst = (Statement) con.prepareStatement(qry);
ResultSet rs = pst.executeQuery(qry);
jTable1.setModel(net.proteanit.sql.DbUtils.resultSetToTableModel(rs));
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, e);
}