package com.csf.practicetest.core; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * Created by fenglei.ma on 2018/4/18. 14:22 */ public class OracleOrMysqlPrepareStatement { /** * <!--oracle 驱动--> * <dependency> * <groupId>com.oracle</groupId> * <artifactId>ojdbc</artifactId> * <version>6</version> * </dependency> */ public static void main(String[] args) { try { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@192.168.100.10:1521:csdb001"; String user = "zhaoshang"; String pwd = "zhaoshang"; // 注册数据库驱动 Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, pwd); String sql = "select * from fin_product_node where secu = ? or secu = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, "601601_SH_EQ"); ps.setString(2, "300471_SZ_EQ"); ResultSet rs = ps.executeQuery(); while (rs.next()) { Integer id = rs.getInt("id"); String secu = rs.getString("secu"); System.out.println("id:" + id + " secu: " + secu); } conn.close(); } catch (Exception e) { e.printStackTrace(); } } /** * <!--mysql 驱动--> * <dependency> * <groupId>mysql</groupId> * <artifactId>mysql-connector-java</artifactId> * <version>5.1.20</version> * </dependency> */ public static void main2(String[] args) { try { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://192.168.250.208:3306/ada-fd?characterEncoding=UTF-8"; String user = "ada_user"; String pwd = "ada_user"; // 注册数据库驱动 Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, pwd); String sql = "select * from hq_stock_tp limit 5"; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { String dt = rs.getString("dt"); String tick = rs.getString("tick"); System.out.println("------> dt:" + dt + " tick: " + tick); } conn.close(); } catch (Exception e) { e.printStackTrace(); } } }