package com.lixs.jdbcoracle;
import java.sql.*;
/**
* 通过jdbc链接数据库oracle
*
* */
public class oracle_jdbc_query {
public static void main(String[] args) {
Connection ct = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2.得到连接
ct = DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
// 3.创建PreparedSatement
ps = ct.prepareStatement("select * from emp where ENAME = ?");
//给? 赋值
ps.setObject(1, "SMITH");
// 4.执行操作
rs = ps.executeQuery();
// 5.根据结果取值
while (rs.next()) {
//用字段名取值
//System.out.println("用户名: "+rs.getString("ENAME"));
//按顺序从1开始取值
System.out.println("用户名: "+rs.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//按倒顺关闭资源
if (rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ct != null){
try {
ct.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
import java.sql.*;
/**
* 通过jdbc链接数据库oracle
*
* */
public class oracle_jdbc_query {
public static void main(String[] args) {
Connection ct = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2.得到连接
ct = DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
// 3.创建PreparedSatement
ps = ct.prepareStatement("select * from emp where ENAME = ?");
//给? 赋值
ps.setObject(1, "SMITH");
// 4.执行操作
rs = ps.executeQuery();
// 5.根据结果取值
while (rs.next()) {
//用字段名取值
//System.out.println("用户名: "+rs.getString("ENAME"));
//按顺序从1开始取值
System.out.println("用户名: "+rs.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//按倒顺关闭资源
if (rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ct != null){
try {
ct.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}