博客
关于我
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/

你可能感兴趣的文章
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>