Oracle 12c JDBC 连接

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

public class Test {
    private static Connection getConn() {
        String driver = "oracle.jdbc.driver.OracleDriver";
        //String url = "jdbc:oracle:thin:@127.0.0.1:1521:pdbmyoracle";// 设置连接字符串
        String url = "jdbc:oracle:thin:@//127.0.0.1:1521/pdbmyoracle";
        String username = "abc";// 用户名
        String password = "abc";// 密码
        Connection conn = null; // 创建数据库连接对象
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }

    private static void query() {
        Connection conn = getConn();
        String sql = "select * from users";
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement(sql);
            // 建立一个结果集,用来保存查询出来的结果

            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                String username = rs.getString("USERNAME");
                System.out.println(username);
            }
            rs.close();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    
    public static void main(String[] args) {
        
        query();
    }

}

Oracle 12c JDBC 连接,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。