i want to get a list of databases stored in mysql and put in java table using command "show databases" via a resultset. but its not working.
DefaultTableModel model=(DefaultTableModel)dbTbl.getModel();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql//localhost/:3306","root","password");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("show databases;");
while(rs.next())
{
String db=rs.getString(1);
model.addRow(new Object[] {db});
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"nahi chalda");
}
I just forgot to add colon after "jdbc:mysql Correct code is:
It works ,......... !!!
That's not the best way to get a list of Databases in JDBC. Here's the way it's done - using MetaData
See also: how to get list of Databases "Schema" names of MySql using java JDBC