• 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

  • 相关阅读:
    IIS常见500错误解决方案
    发送邮件代码
    IIS站点/虚拟目录中访问共享目录(UNC)
    简简单单,一目了然C#与Matlab
    [转载]C#——DataGridView分页功能的实现
    博客之旅
    ASP.Net, Php , Java/Java EE?好困惑
    【转载】DataGridView中虚拟模式(Virtual Mode)用法
    selenium4.0降级为3版本
    web自动化中影响页面定位的场景有哪些?
  • 原文地址:https://www.cnblogs.com/hnrainll/p/2859870.html
Copyright © 2020-2023  润新知