JDBC连接Oracle数据库
1、首先导入oracle的jar包
2、编写java代码
package file; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JDBC { public static void main(String[] args) { String driver = "oracle.jdbc.driver.OracleDriver"; String passwrod = "devel"; String userName = "devel"; String url = "jdbc:oracle:thin:@192.168.1.203:1521:orcl"; String sql = "SELECT * FROM T_ROLE"; try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, userName,passwrod); PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println("ROLE_ID : " + rs.getInt(1) + " ROLE_NAME : " + rs.getString(2)); } // 关闭记录集 if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } // 关闭声明 if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } // 关闭链接对象 if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } }