刚学习的,呵呵
建了个库,存有username和password,java中如何取出这两个字段的值呢???兄弟们,给个片段就可以了,谢谢了挺急的~~~~~~~~~~~~~~~~~

解决方案 »

  1.   

    String sql = "select * from userInfo where id= ?";
    pstmt = conn.prepareStatement(sql);
    pstmt.setInt(1,id);
    ResultSet rs = pstmt.executeUpdate();
    UserInfo user = new UserInfo();
    user.setUsername(rs.getString("username"));
    user.setPassword(rs.getString("password"));
      

  2.   

    连接数据库,得到ResultSet对象rs然后rs.getString("username")获得username的值,password一样。
      

  3.   

    用JDBC或任何一种O/R映射工具都可以读
    给个JDBC的:
    java.util.Properties info = new java.util.Properties();
    info.put ("user", "scott");//假设是Oracle下的scott用户
    info.put ("password", "tiger");//假设密码是tigerConnection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    String url = "jdbc:oracle:thin:@localhost:1521:orcl";//看你自己的SID是多少,我的是orcl
    String username = null;
    String password = null;
    try {
          conn = DriverManager.getConnection(url,info);
          stmt = conn.createStatement();
          rs = stmt.executeQuery("select username, password from 表名");
          while(rs.next()) {
              username = rs.getString("username");
              password = rs.getString("password");
              //这里你可以将username与password进行封装,放到另一个东西里面返回,看具体情况
    } catch(Exception e) {} finally {
      //一些关闭资源(conn、stmt、rs)的操作,去网上找一个好点的,不写了

    太久没写JDBC代码了,大概就那样,自己去网上找比在这问好得多了
      

  4.   

    5楼的兄弟,我没有看懂您new个Perpertis对象有什么用呢??能给我讲讲吗???
      

  5.   


    //连接oracle
    String url="jdbc:oracle:thin:@localhost:1521:orcl"; 
    //orcl为数据库的SID 
    Statement stmt = null; 
    ResultSet rs = null; 
    String username = "test"; 
    String password = "test"; 
    try { 
                          Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); 
                  Connection conn= DriverManager.getConnection(url,username,password); 
          stmt = conn.createStatement(); 
          rs = stmt.executeQuery("select username, password from 表名"); 
          while(rs.next()) { 
              username = rs.getString("username"); 
              password = rs.getString("password"); 
              //这里你可以将username与password进行封装,放到另一个东西里面返回,看具体情况 
    } catch(Exception e) {  } finally {
                        if(rs!=null) 
            rs.close();
                        if(stmt!=null)
                            stmt.close();
                        if(conn!=null)
                            conn.close(); }
    //连接mysql
    String url ="jdbc:mysql://localhost/myDB?username=test&password=test&useUnicode=true&characterEncoding=8859_1"  Statement stmt = null; 
    ResultSet rs = null; 
                    Connection conn = null;
    try { 
                          Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
                  //myDB为数据库名 
                             conn= DriverManager.getConnection(url); 
          stmt = conn.createStatement(); 
          rs = stmt.executeQuery("select username, password from 表名"); 
          while(rs.next()) { 
              username = rs.getString("username"); 
              password = rs.getString("password"); 
              //这里你可以将username与password进行封装,放到另一个东西里面返回,看具体情况 
    } catch(Exception e) { 
                        
    } finally {
                        if(rs!=null) 
            rs.close();
                        if(stmt!=null)
                            stmt.close();
                        if(conn!=null)
                            conn.close(); }
      

  6.   

    21875631  java爱好者进!! 
      

  7.   

    1:建立连接 就是Connection
    2:写好sql
    3 :编译SQL。
    4 :取得结果集。
    5: 从结果集里取得你要的信息。
      

  8.   

     rs = stmt.executeQuery("select username, password from 表名"); 
          while(rs.next()) { 
          username = rs.getString("username"); 
          password = rs.getString("password");