我在做jsp网站和java的小系统时发现在连接数据库,如果每次一个连接数据库时都要写一个连接数据库的代码,现在我想把连接数据库的代码封装起来.封装以后在使用时只要在开头引入就可以了.
我的问题就是引入以后怎么用?

解决方案 »

  1.   

    比如我的封装数据库是:
    package one;
    import java.sql.*;public class DataBase{
    public Connection conn;
    public Statement stmt;
    public ResultSet rs=null;
    public String sqlStr="";

    public DataBase(){
    this.connect();
    }

    public boolean connect(){
    try{
          Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDrive").newInstance();
          String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mybase";
          String user="sa";
          String pwd="fuhui";
          conn=DriverManager.getConnection(url,user,pwd);
          stmt=conn.createStatement();
        }catch(Exception e){
        System.out.println("connect db error  " +e.getMessage());
        return false;
        }
         return true;
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try{
    DataBase db=new DataBase();
    db.connect();
    }catch(Exception ee){
    ee.printStackTrace();
    } }}在查询数据库时怎么用呢?能举出一个例子就可以拉/
    谢谢
    急用!
      

  2.   


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;public class DBConnection {
    public static Connection getConnection() {
    String drivrename = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String dabaseurl = "jdbc:sqlserver://localhost:1433;DatabaseName=mybase";
    String dbUser = "sa";
    String dbPassword = "fuhui";
    Connection con = null; try {
    Class.forName(drivrename);
    con = DriverManager.getConnection(dabaseurl, dbUser, dbPassword);
    return con;
    } catch (java.lang.ClassNotFoundException e) {
    System.err.println(e);
    } catch (SQLException er) {
    System.err.println(er.getMessage());
    } return null;
    }
      

  3.   

    import 包 .DBConnectionprivate Connection con = null;
    PreparedStatement ps = null;
    Connection con = DBConnection.getConnection();//取得链接
      

  4.   

    Connection con = DBConnection.getConnection();
    PreparedStatement ps = null;
    try {
    ps = con
    .prepareStatement("update 表 set 列名1=? where 列名2=?");
    ps.setString(1, **);
    ps.setString(2, **);
    if (ps.executeUpdate()!= 0) {
    return true;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }  finally {
    try{
                    if(ps!=null){
                           ps.close();
                    }
                    if(con!=null){
                           con.close();
                    }               }catch(Exception e)
                   {
                           e.printStackTrace();
                   }
    }
    return false;
    }
      

  5.   

    像2楼那样封装一个DBConnection类,下面给一个应用的实例
    public class Test{
      public static void main(String[] args){
        Connection con;
        Statement st;
        ResultSet rs;
        con=DBConnection.getConnection();//直接通过DBConnection类的getConnection方法获得Connection对象
        if(con==null){
          System.out.println("数据库连接失败");
          return;
        }
        st=con.CreatStatement();
        rs=st.executeQuery("select num,name,age from students where num='007'");
        while(rs.next()){
          System.out.println(rs.getString("num"));
          System.out.println(rs.getString("name");
          System.out.println(rs.getInt("age"));
        }
    }
      

  6.   

    建议楼主参考apache dbUtils项目,它已经封装了database链接pool管理功能,也可以直接拿来用。http://commons.apache.org/dbutils/
      

  7.   

    应用:DataBase db=new DataBase(); 
    if(!db.connect()){
      db.connect();
     } 
    Statement st; 
    ResultSet rs; 
     st=con.CreatStatement(); 
        rs=st.executeQuery("select num,name,age from students where num= '007 '"); 
        while(rs.next()){ 
          System.out.println(rs.getString("num")); 
          System.out.println(rs.getString("name"); 
          System.out.println(rs.getInt("age")); 
        } 
      

  8.   

    二楼跟你写得很清楚了 数据库链接类都封装好了
    //生成链接
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.SQLException; public class DBConnection { 
    public static Connection getConnection() { 
    String drivrename = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 
    String dabaseurl = "jdbc:sqlserver://localhost:1433;DatabaseName=mybase"; 
    String dbUser = "sa"; 
    String dbPassword = "fuhui"; 
    Connection con = null; try { 
    Class.forName(drivrename); 
    con = DriverManager.getConnection(dabaseurl, dbUser, dbPassword); 
    return con; 
    } catch (java.lang.ClassNotFoundException e) { 
    System.err.println(e); 
    } catch (SQLException er) { 
    System.err.println(er.getMessage()); 
    } return null; 

    //调用
    import 包 .DBConnection private Connection con = null; 
    PreparedStatement ps = null; 
    Connection con = DBConnection.getConnection();//取得链接 数据库执行完后记得关闭链接