java mysql纯驱动连接

package SQL;

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

public class JDBCTest {
    
    public static String Driver="com.mysql.jdbc.Driver";
    public static String url="jdbc:mysql://127.0.0.1:3306/test";
    public static String user="root";
    public static String password="333";
    
    public static void main(String[] args) {
        
        Connection con;
        Statement statement;
        ResultSet rs;
        try {
            //加载驱动
            Class.forName(Driver);
            //连接数据库
            con=DriverManager.getConnection(url, user, password);
            
            if(!con.isClosed())
                System.out.println("connect successed");
            
            statement=con.createStatement();
            String sql="select * from tmp";
            
            rs=statement.executeQuery(sql);
            
            while(rs.next())
            {
                String rq=rs.getString("rq");
                System.out.println("rq="+rq);
            }
            
            rs.close();
            con.close();
            
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载失败");
            e.printStackTrace();
        }catch(SQLException e){
            System.out.println("sql执行失败");
            e.printStackTrace();
        }catch(Exception e){
            
        }finally{
            
        }
    }

}

 

posted @ 2014-10-01 23:54  凡尘里的一根葱  阅读(391)  评论(0编辑  收藏  举报