代码之家  ›  专栏  ›  技术社区  ›  Zack King

MySQL-如何将通过select语句从MySQL访问的数据存储到字符串中?[闭门]

  •  -1
  • Zack King  · 技术社区  · 7 年前

    我在MySQL中有一个表,其中包含用户凭据(用户名、密码)和名字、姓氏。

    我将用户名从 Login.java 将Cookie分页到我的 welcome.jsp 页我想做的是:运行 SELECT 语句检索用户的名字 username 他们过去常常登录。这样地:

    SELECT `first_name` FROM table WHERE username = userName;
    

    userName 是用户用于登录到我的应用程序的用户名的变量。此select语句应返回用户的名字。

    我的问题是:如何将其存储到字符串中?

    1 回复  |  直到 4 年前
        1
  •  0
  •   Jay Shankar Gupta    7 年前
    try{  
    Class.forName("com.mysql.jdbc.Driver");  
    Connection con=DriverManager.getConnection(  
    "jdbc:mysql://localhost:3306/sonoo","root","root");  
    //here sonoo is database name, root is username and password  
    Statement stmt=con.createStatement();  
    ResultSet rs=stmt.executeQuery("SELECT first_name FROM table WHERE username = '"+ userName+"';");
    String fname;  
    while(rs.next())  
    fname=new String(rs.getString(1));
    System.out.println("First Name:"+fname);  
    con.close();  
    }
    catch(Exception e){ 
      System.out.println(e);
     }