主界面如下所示:
9
〈5〉关键代码如下:由于代码太多,以下只写出很少的一部分代码 1》对数据库的操作:
public class ConneJdbc {
protected static Connection connection = null; private Connection con = null; private ResultSet rs = null;
private ResultSetMetaData rsmd = null; public ConneJdbc() { try { Class.forName(\ connection = DriverManager.getConnection( \ \
} catch (java.lang.ClassNotFoundException classnotfound) { classnotfound.printStackTrace(); } catch (java.sql.SQLException sql) { sql.printStackTrace(); } }
public Connection getCon() { return connection; }
10
public Vector selectSql(String sql) { Vector vdata = new Vector(); try { rs = connection.prepareStatement(sql).executeQuery(); rsmd = rs.getMetaData(); while (rs.next()) vdata.addElement(rs.getObject(1)); } catch (SQLException e) { e.printStackTrace(); }
return vdata; }
public String selectOne(String sql) { ResultSet rs = null;
ResultSetMetaData rsmd = null; try { rs = connection.prepareStatement(sql).executeQuery(); rsmd = rs.getMetaData(); if (rs.next()) return (String)rs.getObject(1); } catch (SQLException e) { e.printStackTrace(); }
return null; }
public boolean inTable(String user, String pa) throws SQLException { Statement stmt = null;
PreparedStatement pstmt = null; ResultSet rs = null;
String sql = \ \ + pa + \
rs = connection.prepareStatement(sql).executeQuery(); if(rs.next())return true; else return false; } }
2》表格模型的处理:
public class SqlTableModel extends DefaultTableModel { private Connection con = null; private ResultSet rs = null;
private ResultSetMetaData rsmd = null;
public SqlTableModel(String sqlStr, String[] name) { con = new ConneJdbc().getCon();
11
try{
rs = con.prepareStatement(sqlStr).executeQuery(); rsmd = rs.getMetaData();
for(int i=0; iwhile(rs.next()){
Vector vdata = new Vector();
for ( int i = 1 ; i <= rsmd.getColumnCount() ; i ++){ vdata.addElement(rs.getObject(i)); }
addRow(vdata); }
}catch(java.sql.SQLException sql){ sql.printStackTrace(); }finally { try { con.close(); } catch (SQLException e) { e.printStackTrace(); }
} }
3)Updatabase的一部分代码:
public class UpdateDatebase extends ConneJdbc { private Statement stmt = null;
private PreparedStatement pstmt = null; private String infoStr = null; private String sql;
public UpdateDatebase() { }
public boolean insert(DepData data) throws SQLException {
String sql = \into department (depth_name,discription) values ('\
+ data.getDepth_name() + \ + data.getDiscription() + \;
pstmt = connection.prepareStatement(sql); pstmt.execute(); return true; }
public boolean insert(StuData data) throws SQLException { sql = \
+ data.getStu_id() + \ + data.getS_name() + \ + data.getSex() + \ + data.getBirthday() + \
12