package Permit;
import java.io.*;
import java.sql.*;
public class DbBean {
private Connection dbCon;
private String dbURL="";
// a default database URL may be provided here.
private String dbDriver="org.gjt.mm.mysql.Driver";
// Set the database URL.
public void setDbURL(String dbURL) {
this.dbURL = dbURL;
}
// Open a database connection
public boolean connect() throws ClassNotFoundException, SQLException {
Class.forName(this.getDbDriver());
dbCon = DriverManager.getConnection(this.getDbURL());
return true;
}
//Close the database connection.
public void close() throws SQLException {
dbCon.close();
}
// Executes an SQL Query.
public ResultSet execSQLQuery(String sql) throws SQLException {
Statement s = dbCon.createStatement();
ResultSet r = s.executeQuery(sql);
return r;
}
}
An example for a database query with the DbBean class looks like this. In the presented case, a 
list of all company names is extracted from the Company table. 
// Instantiate the database JavaBean class
DbBean dbBean = new DbBean();
// Set the database URL
dbBean.setDbURL("jdbc:mysql://localhost/test_permit?user=&password=");
// Establish a connection
dbBean.connect();
// Execute the SQL query
ResultSet rs = dbBean.execSQLQuery("SELECT ComName FROM Company");
// Now, do something with the result set, e.g. display it!
// Don't forget to close the connection!
dbBean.close();
62 




Java Web Hosting Application Development Using Java Technologies Hosting




 
TotalRoute.net Business web hosting division of Vision Web Hosting Inc. All rights reserved.