标题:[求助] 在MyEclipse中直联oracle方法!
只看楼主
j2ee126
Rank: 1
等 级:新手上路
帖 子:124
专家分:0
注 册:2007-12-22
结帖率:100%
 问题点数:0 回复次数:1 
[求助] 在MyEclipse中直联oracle方法!
我想在在MyEclipse中写一个直联oracle方法,怎么写啊!
搜索更多相关主题的帖子: oracle MyEclipse 中直 
2008-07-18 14:52
luyihuaa5201
Rank: 2
等 级:论坛游民
帖 子:254
专家分:10
注 册:2007-3-25
得分:0 
public class DBAccess {
    private String driver="oracle.jdbc.driver.OracleDriver";
    
    private String url="jdbc:oracle:thin:@localhost:1521:SID";
    
    private String user="userName";
    
    private String pwd="passWord";
    
    private Connection conn;
    private Statement stmt;
    private ResultSet rst;
    
    public DBAccess(){
        try {
            Class.forName(driver);
            conn=DriverManager.getConnection(url,user,pwd);
            stmt=conn.createStatement();
            System.out.println("连接成功");
        } catch (ClassNotFoundException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        
    }
    
    
    public ResultSet query(String sql){
        try {
            rst=stmt.executeQuery(sql);
        } catch (SQLException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        return rst;
    }
    
    
    public int update(String sql){
        int flag=-1;//如果返回-1说明执行失败,如果返回0成功
        try {
            flag=stmt.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        return flag;
    }
    
    public void close(){
        try{
        
        if(rst!=null){
            rst.close();
            rst=null;
            
        }
        
        if(stmt!=null){
            stmt.close();
            stmt=null;
            
        }
        
        if(conn!=null){
            conn.close();
            conn=null;
            
        }
        }catch(SQLException e){
//             TODO 自动生成 catch 块
            e.printStackTrace();
        }
        
    }


    public Connection getConn() {
        return conn;
    }
    public static void main(String[] args) {
        DBAccess access = new DBAccess();
    }
}
2008-07-30 17:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-224344-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.289134 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved