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

    }

  • 相关阅读:
    实例15_C语言绘制万年历
    医生酒精
    实例13_求解二维数组的最大元素和最小元素
    用二维数组实现矩阵转置
    C语言中的typedef跟define的区别
    C语言设计ATM存取款界面
    MyBatis,动态传入表名,字段名的解决办法
    在mybatis执行SQL语句之前进行拦击处理
    使用Eclipse构建Maven的SpringMVC项目
    Debug过程中的mock (及display窗口的使用)
  • 原文地址:https://www.cnblogs.com/siashan/p/3874793.html
Copyright © 2011-2022 走看看