zoukankan      html  css  js  c++  java
  • JDBC数据库连接之JDBC-ODBC

    package com.pkg1;
    
    import java.sql.*;
    
    public class JdbcDemo {
        
        Connection conn = null;
        //Statement st = null;
        PreparedStatement st = null;
        
        public JdbcDemo(){
            this.init();
        }
        
        public boolean init(){
            try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //连接字符串
                this.conn = DriverManager.getConnection("jdbc:odbc:test1", "sa", "123456");  //test1 odbc中的名称
                //this.st = this.conn.createStatement();
                
            }catch(Exception e){
                e.printStackTrace();
                this.close();
                return false;
            }
    
            return true;
        }
    public int runCmd(String sql){ int ret = -1; try { this.st = this.conn.prepareStatement(sql); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if(st == null){ return ret; } try{ ret = st.executeUpdate(sql); }catch(Exception e){ e.printStackTrace(); return ret; } return ret; }
      //String sql = "select * from person where uid=?"
    public ResultSet runQuery(String sql, String uid){ ResultSet rs = null; try { this.st = this.conn.prepareStatement(sql); this.st.setString(1, uid); //设置参数 if(st == null){ return rs; } rs = st.executeQuery(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); return rs; } return rs; } public void close(){ try{ if(st != null){ st.close(); } if(conn != null){ conn.close(); } }catch(Exception ex){ ex.printStackTrace(); } } }
  • 相关阅读:
    剑指 Offer II 005. 单词长度的最大乘积
    中文编程的瓶颈
    Unity TextMeshPro 富文本格式介绍
    centos使用httpd搭建文件下载服务器教程
    开博第一天
    macOS安装brew(Homebrew国内源)
    git命令将代码导出为单个文件
    CPU虚拟化
    指令
    华为公有云服务的主要服务产品
  • 原文地址:https://www.cnblogs.com/guanhao/p/4926144.html
Copyright © 2011-2022 走看看