eclipse java 连接mysql

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import com.mysql.jdbc.Connection;

public class MySql {
    public static void main(String[]args) throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String url="jdbc:mysql://localhost:3306/mysql";
        String username="root";
        String password="root";
         
        Connection con=(Connection) DriverManager.getConnection(url,username,password);
        if(con!=null) {
         
            System.out.println("成功");
      }
        Statement stmt;
        ResultSet res;
        stmt=con.createStatement();
        res=stmt.executeQuery("select *from user");
        while(res.next()){
            String name=res.getString("User");
            System.out.println(name);
        }
        res.close();
    }

}

posted @ 2011-08-17 18:41  glose  阅读(317)  评论(0编辑  收藏  举报