一个applet servlet之间通过序列化对象通讯的例子
源代码在线查看: dbconnection.java~11~
package espc; import java.io.*; import java.sql.*; import java.util.Properties; public class DbConnection { Connection conn=null; Statement stmt=null; ResultSet rset=null; //构造方法 public DbConnection() { } public boolean openConnection() { //----------------------------------------------------------------- //装载数据库的Property文件:其中包括数据库驱动,数据库URL,即用户ID和密码 //----------------------------------------------------------------- Properties prop = new Properties(); try { InputStream is = getClass().getResourceAsStream("Db.properties"); prop.load(is); if(is != null) is.close(); } catch(IOException e) { System.out.println("[DbConnection]打开文件时出现错误!"); } //----------------------------------------------------------- //从property文件中读出数据库Property //----------------------------------------------------------- String jdbc = prop.getProperty("drivers"); String url = prop.getProperty("url"); String user = prop.getProperty("user"); String password = prop.getProperty("password"); //----------------------------------------------------------- //将输入的property输出到Debugging //----------------------------------------------------------- System.out.println(String.valueOf(String.valueOf((new StringBuffer("jdbc=[")).append(jdbc).append("]")))); System.out.println(String.valueOf(String.valueOf((new StringBuffer("url=[")).append(url).append("]")))); System.out.println(String.valueOf(String.valueOf((new StringBuffer("user=[")).append(user).append("]")))); System.out.println(String.valueOf(String.valueOf((new StringBuffer("password=[")).append(password).append("]")))); //----------------------------------------------------------- //加载jdbc驱动 //----------------------------------------------------------- try { Class.forName(jdbc); System.out.println("驱动程序加载成功!"); } catch(ClassNotFoundException e) { System.out.println("JDBC登陆过程中出现错误".concat(String.valueOf(String.valueOf(e.getMessage())))); boolean flag = false; return flag; } //----------------------------------------------------------- //打开数据库connection //----------------------------------------------------------- try { conn = DriverManager.getConnection(url, user, password); System.out.println("数据库连接成功!!"); } catch(SQLException e) { System.out.println("数据库连接过程中出现错误".concat(String.valueOf(String.valueOf(e.getMessage())))); boolean flag1 = false; return flag1; } return true; } public ResultSet executeQuery(String query) throws SQLException { stmt = conn.createStatement(1004, 1007); rset = stmt.executeQuery(query); return rset; } public void executeUpdate(String query) throws SQLException { stmt = conn.createStatement(1004, 1008); stmt.executeUpdate(query); if(stmt != null) stmt.close(); } public void close() throws SQLException { if(conn != null) conn.close(); if(rset != null) rset.close(); if(stmt != null) stmt.close(); } protected void finalize() throws Throwable { close(); } }