博客
关于我
Java 连接mysql的jdbcutil代码
阅读量:400 次
发布时间:2019-03-05

本文共 3544 字,大约阅读时间需要 11 分钟。

老规矩,话不都说,上代码:

package com.meboth.hive.connection.hbase.utils;import java.sql.*;import java.util.*;import java.io.*;public class JdbcUtil {    public static final String DBDRIVER = "com.mysql.jdbc.Driver";    public static final String DBURL = "jdbc:mysql://localhost:3306/my_test";    public static final String DBUSER = "root";    public static final String DBPASS = "";    private Connection con = null;    private PreparedStatement ps = null;    private ResultSet rs = null;    /**     * 初始化连接     */    public void initConnection() {        try {            Class.forName(DBDRIVER);            con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (SQLException e) {            e.printStackTrace();        }    }    /**     * 查询数据     * @param sql     * @param obj     * @return     */    public ResultSet queryData(String sql, Object[] obj) {        String str = "";        try {            if(con==null){                initConnection();            }            ps = con.prepareStatement(sql);            for (int i = 0; i < obj.length; i++) {                ps.setObject(i + 1, obj[i]);            }            rs = ps.executeQuery();        } catch (SQLException e) {            e.printStackTrace();        }        return rs;    }    /**     *添加,修改和删除     * @param sql     * @param obj     * @return     */    public int updateAndDeleteData(String sql, Object[] obj) {        int i = 0;        try {            if(con==null){                initConnection();            }            ps = con.prepareStatement(sql);            for (int j = 0; j < obj.length; j++) {                ps.setObject(j + 1, obj[j]);            }            i = ps.executeUpdate();        } catch (SQLException e) {            e.printStackTrace();        } finally {            closePs();        }        return i;    }    /**     * 关闭ps连接     */    public void closePs(){        if (ps != null) {            try {                ps.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }    /**     * 关闭rs连接     */    public void closeRs(){        if (rs != null) {            try {                rs.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }    /**     * 关闭所有连接     */    public void closeConnction() {        try {            if (ps != null) {                ps.close();            }            if (rs != null) {                rs.close();            }            if (con != null) {                con.close();            }        } catch (SQLException e) {            e.printStackTrace();        }    }    public static void main(String[] args) {        JdbcUtil tj = new JdbcUtil();        /**         String sql = "insert into company values(?,?,?)";         Object[] obj = {3, "baidu", "liyanhong"};         int i = tj.updateAndDeleteData(sql, obj);         System.out.println(i);         **/        String sql = "select * from company ";        Object[] obj = {};        ResultSet rs = tj.queryData(sql, obj);        try {            while (rs.next()) {                int id = rs.getInt(1);                String str = rs.getString(2);                String st = rs.getString(3);                System.out.println(id+"=="+str+"=="+st);            }        } catch (SQLException e) {            e.printStackTrace();        } finally {            tj.closeConnction();        }    }}

查询显示效果:

1==阿里巴巴==马云

2==淘宝==马云,孙正义
3==baidu==liyanhong

转载地址:http://ysyzz.baihongyu.com/

你可能感兴趣的文章
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>
MySQL中的时间函数
查看>>
mysql中的约束
查看>>
MySQL中的表是什么?
查看>>
mysql中穿件函数时候delimiter的用法
查看>>
Mysql中索引的分类、增删改查与存储引擎对应关系
查看>>