Database.java的程序如下
package javaBean ;
import java.io.Serializable;
import java.util.*;
import java.sql.*;
public class Database
{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
ResultSetMetaData rsmd= null;
/** Establishes a connection whith the dadabase server
* @param serverName
* @return (None)-constructor
* @exception SQLException if there is problem connecting
* to the server or accessing StockMarket database
*/
public Database (String serverName) throws SQLException
{
try
{
java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
String connectString = "jdbc
racle:thin
10.1.1.14:1521
ra8";
con = java.sql.DriverManager.getConnection (connectString,"zjgsmmis","zjgsmmis");
stmt = con.createStatement();
}
catch (SQLException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
}
/** Close the connection with the database server
* @param none
* @return void
* @exception SQLException if there is a problom connecting
* to the server
*/
public void close() throws SQLException
{
try
{
if (rs != null)
{
rs.close();
}
if (con != null)
{
con.close();
}
}
catch (SQLException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
}
/** get the dhhm
* @param String dhhm,String tablename
* @return int YHBH or int JSZH
* @exception SQLException,Exception
*/
public int getId(String dhhm,String tablename) throws SQLException ,Exception
{
int YHBH = 0 ;
int JSZH =0;
try
{
String sql="select YHBH,JSZH from TYT_YHDA where DHHM = "+dhhm;
rs=stmt.executeQuery(sql);
while (rs.next()){
YHBH=rs.getInt("YHBH");
JSZH=Integer.parseInt(rs.getString("JSZH"));
}
}
catch (SQLException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
if (tablename.equals("TYT_YHZH"))
{ return JSZH;
}else
{
return YHBH;
}
}
public void updateYhdb (int YHBH,String DBYB,
String DBHM,String DBTXDZ,int DBYJ ) throws RecordNotFoundException
{
if ( !haveRecord("TYT_YHDB",YHBH))
{
throw new RecordNotFoundException("用户编号不存在");
}
try
{
String sql = "update TYT_YHDB set DBYB = '"+DBYB+"', DBHM = '"+DBHM+"',DBTXDZ = '"+DBTXDZ+"',DBYJ = "+DBYJ+" where YHBH= '"+YHBH+"'";
stmt.executeUpdate(sql);
}
catch (SQLException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
}
/**return a customerRec populated with data from
* the Customer and Shares table
* @param String ssn
* @return CustomerRec
* @exception RecordNotFoundException if ssn is not found
* in the Customer table
*/
//YhdbRec 是自己定义的类,是一个结果集
public YhdbRec getYhdb(int YHBH) throws
RecordNotFoundException
{
YhdbRec YhdbR=null;
try
{
if (!haveRecord("TYT_YHDB",YHBH))
{
throw new RecordNotFoundException("记录不存在!");
}
String sql = "select * from TYT_YHDB where YHBH = "+YHBH;
rs = stmt.executeQuery(sql);
while (rs.next())
{
YhdbR = new YhdbRec(rs.getInt("YHBH"),rs.getString("DBYB"),rs.getString("DBHM"),rs.getString("DBLXDH"),rs.getString("DBTXDZ"),rs.getInt("DBYJ"));
}
}
catch (SQLException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
return YhdbR;
}
public YhxxRec getYhxx(int YHBH) throws
RecordNotFoundException
{
YhxxRec YhxxR=null;
try
{
if (!haveRecord("TYT_YHXX",YHBH))
{
throw new RecordNotFoundException("记录不存在!");
}
String sql = "select * from TYT_YHXX where YHBH = "+YHBH;
rs = stmt.executeQuery(sql);
while (rs.next())
{
YhxxR = new YhxxRec(rs.getInt("YHBH"),rs.getString("ZJDZ"),rs.getDate("FZRQ"),rs.getInt("YXQX"),rs.getString("LXDH"),rs.getString("DWYB"),rs.getString("DWHM"),rs.getString("DWLXDH"),rs.getString("DWTXDZ"));
}
}
catch (SQLException e)
{
System.out.println(e);
}
catch (Exception e)
{
System.out.println(e);
}
return YhxxR;
}
private boolean haveRecord(String tableName,int YHBH) {
boolean have = false;
try
{
//stmt = con.createStatement();
String sql="";
if (tableName.equals("TYT_YHZH"))
{
sql = "select * from TYT_YHZH where JSZH = '" +
YHBH+"'";
}else
{
sql="select * from " + tableName +
" where YHBH='" +YHBH +"'" ;
}
rs=stmt.executeQuery(sql);
have=rs.next();
//System.out.println(have);
rs.close();
}catch(SQLException e) {
System.out.println(e);
}
return have;
}
};