0% found this document useful (0 votes)
0 views

JAVA 2 Connectioncode

Uploaded by

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

JAVA 2 Connectioncode

Uploaded by

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

SAVE CODE

try

Class.forName("com.mysql.jdbc.Driver");

Connection con = (Connection)


DriverManager.getConnection("jdbc:mysql://localhost/susms","root","");

Statement std = (Statement) con.createStatement();

String qry ="insert into Students values ('" + txtID.getText() + "','" +


txtName.getText() + "','" + cboGender.getSelectedItem() + "','" + txtAddress.getText()
+ "','" + txtPhone.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);

FILL JTABLE CODE


Page 1 of 6
try

Class.forName("com.mysql.jdbc.Driver");

Connection con = (Connection)


DriverManager.getConnection("jdbc:mysql://localhost/susms","root","");

Statement std = (Statement) con.createStatement();

String qry ="select * from Students ";

Statement pst = (Statement) con.prepareStatement(qry);

ResultSet rs;

rs=pst.executeQuery(qry);

jTable1.setModel(DbUtils.resultSetToTableModel(rs));

catch (Exception e)

JOptionPane.showMessageDialog(this, e);

FILL OBJECT WITH JTABLE CODE


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/susms","root","");

Statement std = (Statement) con.createStatement();

String qry ="select * from Students where ID='" + Table_Click+"'";

Page 2 of 6
Statement pst = (Statement) con.prepareStatement(qry);

ResultSet rs;

rs=pst.executeQuery(qry);

if (rs.next())

String add1 = rs.getString("ID");

txtID.setText(add1);

String add2 = rs.getString("Name");

txtName.setText(add2);

String add3 = rs.getString("Gender");

cboGender.setSelectedItem(add3);

String add4 = rs.getString("Address");

txtAddress.setText(add4);

String add5 = rs.getString("Phone");

txtPhone.setText(add5);

else

JOptionPane.showMessageDialog(this, "not found data");

Page 3 of 6
}

catch (Exception e)

JOptionPane.showMessageDialog(this, e);

UPDATE CODE
try

Class.forName("com.mysql.jdbc.Driver");

Connection con = (Connection)


DriverManager.getConnection("jdbc:mysql://localhost/susms","root","");

Statement std = (Statement) con.createStatement();

String qry ="update students set Name ='" + txtName.getText() + "', Gender='"
+ cboGender.getSelectedItem()+"', Address='" + txtAddress.getText()+ "', Phone='" +
txtPhone.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");

Page 4 of 6
}

catch (Exception e)

JOptionPane.showMessageDialog(this, e);

DELETE CODE
try

Class.forName("com.mysql.jdbc.Driver");

Connection con = (Connection)


DriverManager.getConnection("jdbc:mysql://localhost/susms","root","");

Statement std = (Statement) con.createStatement();

String qry ="delete from students 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)

Page 5 of 6
{

JOptionPane.showMessageDialog(this, e);

Page 6 of 6

You might also like