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

  • 相关阅读:
    博客园的第一篇博客
    I-如何办好比赛
    塞特斯玛斯塔
    字典序最大的子序列
    百练POJ 1657:Distance on Chessboard
    百练POJ2750:鸡兔同笼
    HDU3790最短路径问题
    HDU 2544最短路Dijkstra算法
    快速幂【倍增+二分】
    树的高度
  • 原文地址:https://www.cnblogs.com/hnrainll/p/2859870.html
Copyright © 2011-2022 走看看