六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 76|回复: 0

jdbc

[复制链接]

升级  96%

12

主题

12

主题

12

主题

童生

Rank: 1

积分
48
 楼主| 发表于 2013-1-27 04:47:30 | 显示全部楼层 |阅读模式
package test;

import java.sql.*;

public class jdbc {
    String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    String theUser = "admin";
    String thePw = "manager";
    Connection c = null;
    Statement conn;
    ResultSet rs = null;

    public jdbc() {
        try {
           
            c = DriverManager.getConnection(dbUrl, theUser, thePw);
            conn = c.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

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

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

    public static void main(String[] args) {
        ResultSet rs;
        jdbc conn = new jdbc();
        rs = conn.executeQuery("select * from test");
        try {
            while (rs.next()) {
                System.out.println(rs.getString("id"));
                System.out.println(rs.getString("name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表