• java 原生PraparedStatement操作数据库


    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();
            }
        }
    }
  • 相关阅读:
    vue生命周期钩子函数
    mongodb window安装配置
    git 添加远程仓
    webpack + vue + node 打造单页面(入门篇)
    git 命令
    javascript 利用FileReader和滤镜上传图片预览
    javascript 一些特殊的字符运算
    Es6 Symbol.iterator
    配置 github 上的程序
    Redis 的数据类型
  • 原文地址:https://www.cnblogs.com/xiaolei2017/p/8874841.html
Copyright © 2020-2023  润新知