Code:
package librarySystemV5;
import java.sql.*;
import java.util.*;
public class DatabaseTableViewer {
private static final String DB = "mytestdb",
TABLE_NAME = "MEMBER",
HOST = "jdbc:mysql://db_host:3306/",
ACCOUNT = "dbadmin",
PASSWORD = "dbadmin",
DRIVER = "jdbc:mysql://localhost:3306/";
public static void main (String[] args) {
try {
// authentication properties
Properties props = new Properties();
props.setProperty("user", ACCOUNT);
props.setProperty("password", PASSWORD);
// load driver and prepare to access
Class.forName(DRIVER).newInstance();
Connection con = DriverManager.getConnection(
HOST + DB, props);
Statement stmt = con.createStatement();
// execute select query
String query = "SELECT * FROM " + TABLE_NAME + ";";
ResultSet table = stmt.executeQuery(query);
// determine properties of table
ResultSetMetaData meta = table.getMetaData();
String[] colNames = new String[meta.getColumnCount()];
Vector[] cells = new Vector[colNames.length];
for( int col = 0; col < colNames.length; col++) {
colNames[col] = meta.getColumnName(col + 1);
cells[col] = new Vector();
}
// hold data from result set
while(table.next()) {
for(int col = 0; col < colNames.length; col++) {
Object cell = table.getObject(colNames[col]);
cells[col].add(cell);
}
}
// print column headings
for(int col = 0; col < colNames.length; col++)
System.out.print(colNames[col].toUpperCase() + "\t");
System.out.println();
// print data row-wise
while(!cells[0].isEmpty()) {
for(int col = 0; col < colNames.length; col++)
System.out.print(cells[col].remove(0).toString()
+ "\t");
System.out.println();
}
}
// exit more gently
catch(Exception e) {
e.printStackTrace();
}
}
}
I got erorr like this
[HTML]
java.lang.Class NotFoundExcepti on: jdbc:mysql://localhost:3306/
at java.lang.Class .forName0(Nativ e Method)
at java.lang.Class .forName(Unknow n Source)
at librarySystemV5 .DatabaseTableV iewer.main(Data baseTableViewer .java:22)
[/HTML]
I used Eclipse IDE,
Please any body help me, This is for reading the data from table and print the out put on console witht the propper table styles.
Comment