本帖最后由 Mguang 于 2010-11-23 22:03:50 编辑

解决方案 »

  1.   


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;public class SimpleJdbc {
    public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    // 1 load the db driver
    Class.forName("com.mysql.jdbc.Driver");
    // 2 get the database conncetion
    Connection conn = DriverManager.getConnection(
    "jdbc:mysql://127.0.0.1:3306/zheng", "root", "root");
    if (conn != null) {
    System.out.println("you are already connect the mysql database!");
    } else {
    System.out.println("Sorry you are not connect the mysql database!");
    }
    //insertARow(conn);
    select(conn);
    conn.close();
    } private static void select(Connection conn) throws SQLException {
    // TODO Auto-generated method stub
    String sql = "select * from user_list";
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    while(rs.next()) {
    System.out.println(rs.getString(1));//这里写个例子,只是取得了第一列的值
    //System.out.println(rs.getString("列名"));
    }

    rs.close();
    stmt.close();
    conn.close();
    }// private static void insertARow(Connection conn) throws Exception {
    // String sql3 = "select * from user_list";
    // Statement stmt = conn.createStatement();
    // int erc3 = stmt.executeUpdate(sql3);
    // // System.out.println("effected row count:"+effectedRowCount);
    // System.out.println("erc3:" + erc3);
    // }
    }