zoukankan      html  css  js  c++  java
  • jdbc连接demo

    package test;
    
    import java.sql.*;
    
    public class JdbcDemo {
        /**
         * 加载驱动
         * */
        static {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 获取数据库连接
         * */
        public static Connection getConnection() {
            String url = "jdbc:mysql:172.0.0.1:3306/test";
            String username = "root";
            String password = "1234";
            Connection con = null;
            try {
                con = DriverManager.getConnection(url, username, password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return con;
        }
    
        /**
         * 获取statement对象,操作数据库,处理返回结果
         * */
        public static void process() {
            Connection con = getConnection();
            PreparedStatement ps = null;
            ResultSet rs = null;
            String sql = "";
            try {
                ps = con.prepareStatement(sql);
                if (ps.execute()) {
                    rs = ps.getResultSet();
                } else {
                    int i = ps.getUpdateCount();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                close(rs, ps, con);
            }
        }
    
        /**
         * 处理返回结果集
         * */
        public static void printResultSet(ResultSet rs) {
            if (rs == null) {
                return;
            }
            try {
                ResultSetMetaData meta = rs.getMetaData();
                int cols = meta.getColumnCount();
                StringBuffer b = new StringBuffer();
                while (rs.next()) {
                    for (int i = 1; i <= cols; i++) {
                        b.append(meta.getColumnName(i) + "=");
                        b.append(rs.getString(i) + "/t");
                    }
                    b.append("/n");
                }
                System.out.print(b.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 关闭连接
         * */
        public static void close(ResultSet rs, Statement stm, Connection con) {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (stm != null) {
                    rs.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    rs.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    source:http://blog.csdn.net/le5yo/article/details/6433906

  • 相关阅读:
    该伙伴事务管理器已经禁止了它对远程/网络事务的支持
    HDU 4883 TIANKENG’s restaurant (贪心)
    Android:创建可穿戴应用
    debian支持ll命令
    mongodb进阶一之高级查询
    Hadoop之——又一次格式化hdfs系统的方法
    J2EE的13个规范之(二) JDBC 及其使用
    2015欧冠决赛--脑力劳动结硕果
    运行计划之误区,为什么COST非常小,SQL却跑得非常慢?
    QVariant与自定义数据类型转换的方法
  • 原文地址:https://www.cnblogs.com/hnrainll/p/2859870.html
Copyright © 2011-2022 走看看