JAVA 数据库操作步骤:
1:注册驱动程序
静态方式:OracleDriver od = new OracleDriver();
动态方式:Class forName("oracle:jdbc:driver:OracleDriver").newInstance(); 利用了反射机制,运行时确定对象,括号内为对象在Jar包中的完整路径;
2:构建连接
Connection conn = DriverManager.getConnection(String url);
例如:url = "jdbc:oracle:thin:@127.0.0.1:test";
3 :创建执行语句
PreparedStatement ps = conn.preparedStatement("select * from user");
ResultSet rs = ps.execQuary();
或
Statement st = conn.createStatement();
ResultSet rs = st.execQuary("select * from user");
或
PreparedStatement ps = conn.preparedStatement();
ResultSet rs = ps. execQuary(String sql);
注: 批处理SQL 语句
ps.addBatch(String sql);
ps.executeBatch();
4 获取数据
rs.next();
System.out.println(rs.getString(1), rs.getString(2));