码迷,mamicode.com
首页 > 数据库 > 详细

Java JDBC

时间:2014-11-09 16:39:26      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   java   sp   for   

1、JDBC源码如下:

 

package nankang.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {

        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
        String user = "phonesurvey";
        String password = "world";
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;

        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);

            String sql = "select * from Agent";
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()) {
                String agentId = rs.getString("AgentId");

                System.out.println(agentId);
            }

            System.out.println("成功");
        } catch (Exception e) {
            System.out.println(String.format("失败,%s", e.getMessage()));
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (pstm != null) {
                try {
                    pstm.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

 

 

 

 

2、

 

 

 

3、

Java JDBC

标签:style   blog   io   color   ar   os   java   sp   for   

原文地址:http://www.cnblogs.com/sshoub/p/4085284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!