Jump to content

Mysql connectivity to java(Netbeans)


debakun

Recommended Posts

So i have created a basic program on retrieving a table from mysql(named "book") onto a table in java (name "buklau").

The name of the database is "12sci".

I cant get it to work! Pls help me out. Thank you.

 

Heres the code on the retrieve button:

 

DefaultTableModel model= (DefaultTableModel)buklau.getModel();
try{
Class.forName("java.sql.Driver");
Statement stmt = con.createStatement();
String query="SELECT * FROM book";
ResultSet rs=stmt.executeQuery(query);
while(rs.next()){
String nm=rs.getString("name");
String ph=rs.getString("phone");
String ct=rs.getString("city");
model.addRow(new Object[]{nm,ph,ct});
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "e");
}
heres the pic of the msql database
Untitled.png
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...