eclipse jdbc最基本


import java.sql.*;
public class TestJDBC {
    public static String URL="jdbc:mysql://localhost:3306/test";
    public static String USER="root";
    public static String PWD="";

    public static void main(String[] args)  {
        Connection conn=null;
        Statement stmt=null;
        ResultSet res=null;
        try{
        String driverName=null;
        driverName="com.mysql.jdbc.Driver";
        Class.forName(driverName);
        
        conn=DriverManager.getConnection(URL, USER, PWD);
        stmt=conn.createStatement();
        res=stmt.executeQuery("select * from counselor");
        
        while(res.next()){
            System.out.println(res.getString("first_name"));
        }
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try{
                if(res!=null)
                    {res.close();
                     res=null;}
                if(stmt!=null)
                    {stmt.close();
                     stmt=null;}
                if(conn!=null)
                    {conn.close();
                     conn=null;}
            }catch(SQLException e){
                e.printStackTrace();
            }
        }

    }

}

posted on 2012-03-26 06:19  friday295  阅读(170)  评论(0编辑  收藏  举报

导航