用JSP来链接数据库(DB类)

 
用jdbc-odbc桥来连接,不过这可是牺牲了速度来的。用jdbc-odbc桥的话,和连接access是一样的,先要设置一个数据源DNS,然后就用
  import java.sql.*;

public class Dblei {
    public Dblei() {
    }
    //直链接
    //private Stringurl= "jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=UserInfo";
    //private String user = "sa";
    //private String password = "";
    public Connection getConn()
    {
        try {
            //Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 直链接
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException ex) {
           System.out.println(ex.getMessage());
        }
        try {
           // return DriverManager.getConnection(url, user, password);  直连接
           Connection conn= DriverManager.getConnection("jdbc:odbc:test");
           return conn;
       } catch (SQLException ex1) {
            return null;
        }
    }
    //关闭链接
    public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection conn)
    {
        if(rs!=null)
        {
            try {
                rs.close();
            } catch (SQLException ex1) {
                System.out.println(ex1.getMessage());
            }
        }
        if(pstmt!=null)
        {
            try {
                pstmt.close();
            } catch (SQLException ex2) {
                System.out.println(ex2.getMessage());
            }
        }
        if(conn!=null)
        {
            try {
                conn.close();
            } catch (SQLException ex3) {
                System.out.println(ex3.getMessage());
            }
        }
    }
    //测试数据库是否连接?
    public static void main(String args[]){
        Dblei d=new Dblei();
        if(d.getConn()!=null){
        System.out.print("ok?");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值