zoukankan      html  css  js  c++  java
  • jdbc 简单连接

    package itcast;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class Base {

        /**
         * @param args
         */
        public static void main(String[] args) {
        
        }

        static void template() throws Exception {
            String url = "jdbc:mysql://localhost:3306/test";
            String user = "root";
            String password = "";
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                // 1注册驱动
                Class.forName("com.mysql.jdbc.Driver");
                // 2建立链接
                conn = DriverManager.getConnection(url, user, password);
                // 3创建语句
                st = conn.createStatement();
                // 4 执行语句
                rs = st.executeQuery("select * from user");
                // 5处理结果
                while (rs.next()) {
                    System.out.println(rs.getObject(1) + " " + rs.getObject(2)
                            + " " + rs.getObject(3) + " " + rs.getObject(4)
                            + " " + rs.getObject(5) + " ");
                }
            } finally {
                try {
                    if (rs != null) {
                        rs.close();
                    }
                } finally {
                    try {
                        if (st != null) {
                            st.close();
                        }
                    } finally {
                        if (conn != null) {
                            conn.close();
                        }
                    }
                }
            }
        }

    }

  • 相关阅读:
    c++ 虚继承与继承的差异 (转)
    主题:PageRank解释
    (转)开源爬虫larbin分析
    Django随笔
    原生爬虫小Demo
    SVN
    Python的正则表达式与JSON
    类库 方法 模块等
    笔记
    自动补全Typeahead
  • 原文地址:https://www.cnblogs.com/siashan/p/3874793.html
Copyright © 2011-2022 走看看