Docsity

Java MySQL Database Interaction Code Snippets, Study notes of Java Programming

Toggle sidebar
bg1
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
{
Page 1 of 6
pf3
pf4
pf5

Partial preview of the text

Download Java MySQL Database Interaction Code Snippets and more Study notes Java Programming in PDF only on Docsity!

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 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); }

txtAddress.setText(add4); String add5 = rs.getString("Phone"); txtPhone.setText(add5); } else { JOptionPane.showMessageDialog(this, "not found data"); } } 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"); } } 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","");