Thực hiện viết mã cho công cụ phần mềm tích hợp CSDL bằng ngôn ngữ lập trình JAVA. JAVA là ngôn ngữ lập trình hƣớng đối tƣợng đƣợc sử dụng phổ biến nhất hiện nay trong việc xây dựng các ứng dụng.
Một số modul trong chƣơng trình:
- Modul kết nối đến các CSDL:
package dbinterface; import java.sql.*; import java.util.*;
public interface Database {
public Vector getTableNames(String schema); public Connection getConnection();
public boolean executeStmt(String sql) throws Exception; public ResultSet getResulst(String sql);
public void closeConnection();
public Vector getColumnslist(String table); public ResultSet getResulst_with_con(String sql); }
// Modul kết nối đến CSDL Mysql server:
package dbinterface; import java.sql.*; import java.util.*;
public class MySQLinterface implements Database{ public Connection con;
public String uname; public String password;
security.PasswordEncDec passdec = new security.PasswordEncDec(); public MySQLinterface(String con,String username,String pass){
con_string = con; uname = username ;
password = passdec.decrypt(pass) ; }
public Connection getConnection() { Connection con = null;
try {
String driver = "com.mysql.jdbc.Driver"; Class.forName(driver);
//String url = "jdbc:mysql://localhost/test"; String url = con_string;
System.out.println(url + "--- " + uname +" -- " + password); con =DriverManager.getConnection( url, uname, password ); } catch (Exception e) {
System.out.println("Error loading MYsql Connection " + e.toString()); }
return con; }
public void closeConnection() {
try{
con.close();
}catch(Exception e) {
System.out.println("Error closing mySQL " + e.toString()); }
}
public boolean executeStmt(String sql)throws Exception {
Connection pcon = getConnection(); Statement st = pcon.createStatement(); ok = st.execute(sql); pcon.commit(); pcon.close(); return ok; }
public ResultSet getResulst_with_con(String sql) { ResultSet rs=null; try{ con = getConnection(); Statement stmt = con.createStatement(); rs = stmt.executeQuery(sql); }catch(Exception e) {
System.out.println("Error generating Result - " + sql + e.toString()); }
return rs; }
public ResultSet getResulst(String sql) { ResultSet rs=null; try{ Statement stmt = con.createStatement(); rs = stmt.executeQuery(sql); }catch(Exception e) {
System.out.println("Error generating Result - " + sql + e.toString()); }
return rs; }
{
con = getConnection(); Vector vec = new Vector();
//vec.add(new String("MYSQLtest")); try{
String sql = "SELECT TABLE_NAME FROM information_schema.`TABLES` where TABLE_SCHEMA='" +schema+"' ";
ResultSet rs= getResulst(sql); while(rs.next())
{
System.out.println("getting table names"); vec.add(new String(rs.getString(1))); }
}catch(Exception e) {
System.out.println(" Error getting table names MYSQL " + e.toString()); }
return vec; }
public Vector getColumnslist(String table) {
Vector vec = new Vector(); try{
con = getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("DESCRIBE " + table); DatabaseMetaData dbm = con.getMetaData();
ResultSet rs1 = dbm.getColumns(null, "%", table, "%"); while (rs1.next()) {
String col_name = rs1.getString("COLUMN_NAME"); String data_type = rs1.getString("TYPE_NAME"); vec.add(new String(col_name + ":" + data_type)); int data_size = rs1.getInt("COLUMN_SIZE"); int nullable = rs1.getInt("NULLABLE");
System.out.print(col_name + "\t" + data_type + "(" + data_size + ")" + "\t"); if (nullable == 1) { System.out.print("YES\t"); } else{ System.out.print("NO\t"); } System.out.println(); } closeConnection(); }catch(Exception e) {
System.out.println(" Error getting colomn names " + e.toString()); }
return vec; }
}