JDBC 查询

//查询""SMITH"的empno
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



public class TestJdbc2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载数据库驱动类
        Class.forName("oracle.jdbc.OracleDriver");
        //2.获取数据库连接对象
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
        String user = "SCOTT";
        String password = "tiger";        
        
        Connection connection = DriverManager.getConnection(url, user, password);
        
        //3.创建sql字符串
        String sql = "select * from emp where ename = ?";
        //4.创建preparedStatement对象
        PreparedStatement pS = connection.prepareStatement(sql);
        //5.获取结果集
        pS.setString(1, "SMITH");
        ResultSet rs = pS.executeQuery();
        //6.遍历
        while(rs.next()){
            System.out.println(rs.getInt("empno"));
        }
        //7.释放资源
        rs.close();
        pS.close();
        connection.close();
    }
}

 

posted @ 2019-04-22 18:55  无声凉薄  阅读(145)  评论(0编辑  收藏  举报