随笔-10  评论-5  文章-0  trackbacks-0

import java.sql.*;

/**
 * Created by IntelliJ IDEA.
 * User: tacimoto
 * Date: 2006-12-29
 * Time: 17:46:03
 * To change this template use File | Settings | File Templates.
 */
public class tacimoto {
    final String oracle_conn_str = "jdbc:oracle:thin:@192.168.0.50:1521:orcl";
    String Usr = "sjs";
    String pwd = "sjs";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    public tacimoto() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            conn = DriverManager.getConnection(oracle_conn_str, Usr, pwd);
            stmt = conn.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean executeUpdate(String sql) {
        try {
            stmt.executeUpdate(sql);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public ResultSet executeQuery(String sql) {
        rs = null;
        try {
            rs = stmt.executeQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }

    public void close() {
        try {
            conn.close();
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.close();
                stmt.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String [] args) {
        tacimoto tac = new tacimoto();
        ResultSet rs = tac.executeQuery("select * from ROLE");
        try
        {
            System.err.print("NAME"+"     ");
            System.err.println("DES");
            System.err.println("-----------------------------------------------------------");
            if(rs != null)
            while(rs.next())
            {
                System.out.print(rs.getString("name")+"     ");
                System.out.println(rs.getString("des"));
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}

我用的开发工具是IDEA5.1,数据库是Oracle 10G。
注意:要把oracle的连接驱动加到工程中,否则 Class.forname会找不到类。
把驱动复制到lib中,然后settings--> module--->把类加到工程中即可。我所用的驱动叫:ojdbc14.jar

posted on 2006-12-29 18:51 bismarck 阅读(216) 评论(0)  编辑 收藏 引用 所属分类: java
只有注册用户登录后才能发表评论。