• mysql jdbc的使用


      先创建一个库petdb,  再创建一个表pettable,在里面加入数据。
    使用jdbc读出。

    import java.sql.*;
    
    /**
     * Created by chuiyuan on 2/6/16.
     */
    public class mysql {
        /**
         * JDBC driver name and database URL
         */
        static final String DB_NAME="petdb";
        static final String TABLE_NAME="pettable";
        static final String JDBC_DRIVER ="com.mysql.jdbc.Driver";
        static final String DB_URL = "jdbc:mysql://localhost/"+DB_NAME;
        /**
         * database credentials
         */
        static final String USER = "petuser";
        static final String PASSWD = "petpwd";
    
        public static void main(String [] args){
            Connection conn = null ;
            Statement stmt = null;
            try {
                /**
                 * register jdbc driver
                 */
                Class.forName(JDBC_DRIVER);
                /**
                 * open a connection
                 */
                System.out.println("Connect to databse ....");
                conn = DriverManager.getConnection(DB_URL, USER, PASSWD);
                /**
                 * execute a query
                 */
                System.out.println("creating statement");
                stmt = conn.createStatement() ;
                String sql;
                sql = "select * from "+ TABLE_NAME;
                ResultSet rs = stmt.executeQuery(sql);
    
                /**
                 * extract data from result set
                 */
                while (rs.next()){
                    //retrive by last column name
                    String name = rs.getString("name");
                    String species = rs.getString("species");
                    //Date birth = rs.getDate("birth");
                    Date death = rs.getDate("death") ;
                    //display
                    System.out.print("name:"+ name);
                    System.out.print(", species:"+ species);
                    //System.out.print(", birth:"+birth);
                    System.out.println(", death:"+ death);
                }
                /**
                 * clean up environment
                 */
                rs.close();
                stmt.close();
                conn.close();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                /**
                 * used to close resources
                 */
                try {
                    if (stmt!=null){
                        stmt.close();
                    }
                }catch (SQLException se){
                    se.printStackTrace();
                }
                try {
                    if (conn!=null){
                        conn.close();
                    }
                }catch (SQLException se1){
                    se1.printStackTrace();
                }
            }
        }
    
    }
    
  • 相关阅读:
    那些花儿
    RIATest 安装与破解
    是什么让你的ExtJS应用程序运行缓慢?
    企业级系统架构的理解
    ExtJS 4.0.1发布:提高性能和错误修复
    ExtJS 4动态加载组件
    Step by Step WebMatrix网站开发之二:使用WebMatrix(3)
    四大类NoSQL数据库
    ExtJS 4中动态加载的路径设置
    性能提高和支持IE 9的新的ExtJS发布版:4.02和3.4
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/5184269.html
Copyright © 2020-2023  润新知