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();
                        }
                    }
                }
            }
        }

    }

  • 相关阅读:
    几个论坛上看到的2015小米笔试题
    Line(扩展欧几里得)
    MapReduce编程之倒排索引
    annotation(@Retention@Target)详解
    【JEECG技术文档】JEECG平台对外接口JWT应用文档V3.7.2
    jeecg 模糊查询
    jeecg下实现自动默认模糊查询
    The packaging for this project did not assign a file to the build artifact
    Maven添加本地Jar包
    maven 如何引入本地jar包
  • 原文地址:https://www.cnblogs.com/siashan/p/3874793.html
Copyright © 2011-2022 走看看