我写的一个bean
package mydb;
import java.sql.*;
public class conn
{
String DBDriver = "org.gjt.mm.mysql.Driver";
String ConnStr = "jdbc:mysql://localhost:3306/net";
Connection conn = null;
public conn() {
try
{
Class.forName(DBDriver);
}
catch (java.lang.ClassNotFoundException error)
{
System.err.println(error.getMessage());
}
} public Connection getconn() {
try
{
conn = DriverManager.getConnection(ConnStr,"","");
return conn;
}
catch (SQLException errorc)
{
System.err.println(errorc.getMessage());
return null;
}
} public void close() {
try {
conn.close();
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}

解决方案 »

  1.   

    我写了一个bean:
    package dbbean;
    import java.sql.*;
    import java.io.*;
    public class dbbean{
        String sDBDriver="oracle.jdbc.driver.OracleDriver";
        Connection conn=null;
        ResultSet rs=null;
        
        //构造函数
        public dbbean(){
            try{
                Class.forName(sDBDriver);
            }catch(java.lang.ClassNotFoundException e){
                System.err.println("dbbean():  "+e.getMessage());
            }
        }    public ResultSet executeQuery(String sqlStr){
            rs=null;
            try{
                conn=
                DriverManager.getConnection"jdbc:oracle:thin:@lmz:1521:labora",
                                                              "szfao","szfao");
         Statement stmt=conn.createStatement();
       rs=stmt.executeQuery(sqlStr);
            }catch(SQLException e){
                System.err.println("executeQuery():  "+e.getMessage());
            }
            return rs;
        }    public int executeInsert(String sqlStr){
            try{
                conn=DriverManager.getConnection
                     ("jdbc:oracle:thin:@lmz:1521:labora","szfao","szfao");
       Statement stmt=conn.createStatement();
       stmt.executeUpdate(sqlStr);
       return 1;//成功
            }catch(SQLException e){
                System.err.println("executeInsert():  "+e.getMessage());
       return 0;//失败
            }
        }    public int executeUpdate(String sqlStr){
            try{
                conn=DriverManager.getConnection
                      ("jdbc:oracle:thin:@lmz:1521:labora","szfao","szfao");
       Statement stmt=conn.createStatement();
       stmt.executeUpdate(sqlStr);
       return 1;//成功
            }catch(SQLException e){
       System.err.println("executeInsert():  "+e.getMessage());
       return 0;//失败
            }
        }    public int executeDelete(String sqlStr){
            try{
                conn=DriverManager.getConnection
                        ("jdbc:oracle:thin:@lmz:1521:labora","szfao","szfao");
       Statement stmt=conn.createStatement();
             stmt.executeUpdate(sqlStr);
       return 1;//成功
            }catch(SQLException e){
       System.err.println("executeDelete():  "+e.getMessage());
       return 0;//失败
            }
        }
    }