2013年7月14日-Java连接Oracle数据库-阿里云开发者社区

开发者社区> 数据库> 正文
登录阅读全文

2013年7月14日-Java连接Oracle数据库

简介:

DataBaseConn:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package query;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

/**
 *
 * @author Administrator
 */
public class DataBaseConn {
    public DataBaseConn(){}
    private static String url = "jdbc:oracle:thin:@localhost:1521:ccgis";
    private static String name = "name";
    private static String password = "password";
    
    public static Connection  getConn(){
        Connection con = null;
        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(url,name,password);
        }
        catch(Exception e){
        }
        return con;
    }
    
    public static void  close(Connection con,Statement sm,ResultSet rs){
        try{
            if(con != null){
                con.close();
            }
            if(sm !=null){
                sm.close();
            }
            if(rs != null){
                rs.close();
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}


GetData:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package query;

/**
 *
 * @author Administrator
 */
public class GetData {
    public String name;
    public String phone;
    public String address;
    
    public String getName(){
        return this.name;
    }
    
    public String getPhone(){
        return this.phone; 
    }
    
    public String getAddress(){
        return this.address;
    }
    
    public void setName(String name){
        this.name = name;
    }
    
    public void setPhone(String phone){
        this.phone = phone;
    }
    
    public void setAddress(String address){
        this.address = address;
    }
}


Query:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package query;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import query.DataBaseConn;
import query.GetData;

/**
 *
 * @author Administrator
 */
public class Query {
    public ArrayList findAll(){
        Connection con = null;
        Statement sm = null;
        ResultSet rs = null;
        String sql = "select * from 用户名.表名";
        ArrayList list = new ArrayList();
        
        try{
            con = DataBaseConn.getConn();
            sm = con.prepareStatement(sql);
            rs = sm.executeQuery(sql);
            while(rs.next()){
                GetData obj = new GetData();
                obj.setName(rs.getString(1));
                obj.setPhone(rs.getString(2));
                obj.setAddress(rs.getString(3));
                list.add(obj);
            }
            }
        catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                DataBaseConn.close(con, sm, rs);
            }catch(Exception e){
                e.printStackTrace();
            }
            return list;
        }
    }
    
    public static void main(String[] args){
        Query q = new Query();
        ArrayList list = q.findAll();
        
        for(int i = 0;i <list.size();i++){
            GetData gd = (GetData)list.get(i);
            System.out.println(gd.getName()+"\t"+gd.getPhone()+"\t"+gd.getAddress());
        }
     }
}


转载:http://blog.csdn.net/foreverling/article/details/9325977

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享: