博客
关于我
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实现成绩排名
查看>>
Mysql客户端中文乱码问题解决
查看>>
mysql导入数据库出现:Incorrect string value: '\xE7\x82\xB9\xE9\x92\x9F' for column 'chinese' at row 1...
查看>>
Mysql工作笔记006---Mysql服务器磁盘爆满了_java.sql.SQLException: Error writing file ‘tmp/MYfXO41p‘
查看>>
MySQL底层概述—5.InnoDB参数优化
查看>>
MySQL底层概述—6.索引原理
查看>>
Mysql建立中英文全文索引(mysql5.7以上)
查看>>
Mysql当前列的值等于上一行的值累加前一列的值
查看>>
MySQL当查询的时候有多个结果,但需要返回一条的情况用GROUP_CONCAT拼接
查看>>
MySQL必知必会(组合Where子句,Not和In操作符)
查看>>
MySQL必知必会总结笔记
查看>>
MySQL快速入门——库的操作
查看>>
mysql快速复制一张表的内容,并添加新内容到另一张表中
查看>>
mysql快速查询表的结构和注释,字段等信息
查看>>
mysql怎么删除临时表里的数据_MySQL中关于临时表的一些基本使用方法
查看>>
mysql性能优化
查看>>
MySQL性能优化必备25条
查看>>
Mysql性能优化(3):分析执行计划
查看>>
MySQL性能测试及调优中的死锁处理方法
查看>>
mysql性能测试工具选择 mysql软件测试
查看>>