java - android tomcat读取servlet字符串文本

标签 java android tomcat jdbc tomcat7

我是 tomcat 和 android 的新手,所以我正在尝试读取 servlet 文件,但有一些错误.. 需要帮助。谢谢! (我想做的是从 tomcat 数据库中读取名称)。

public void doPost(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException {
    Statement stmt;
    ResultSet rs=null;

    String name="";
    try {
      stmt = con.createStatement();
      rs = stmt.executeQuery("SELECT * FROM team7");
      while(rs.next()){
      name=rs.getString("");

      }
    }
    catch (Exception e) {     }
    try {       rs.close();     }    catch (SQLException e) {     }

    PrintWriter out = resp.getWriter();
    out.println(name);

}
}

最佳答案

步骤是:

  1. 加载合适的驱动(驱动类放在WEB-INF/lib文件夹下)
  2. 创建一个Connection 对象。
  3. 执行语句
  4. 遍历 ResultSet
  5. 获取列中特定行的值。

现在,看看您的代码:

while(rs.next()){
   name=rs.getString(""); // you didn't provide the column name here
}

理想情况下,如果列的名称是 name,那么您的代码应该是:

while(rs.next()){
   name=rs.getString("name"); // here the name of the column is name
}

您可以使用以下任一方法:

getString(columnIndex) :

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

Parameters:

columnIndex - the first column is 1, the second is 2, ...

getString(columnLabel) :

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

Parameters:

columnLabel - the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column

遍历 Oracle tutorial了解更多。

关于java - android tomcat读取servlet字符串文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17637928/

相关文章:

Java POI删除单元格

java - 如果我只需要查找一次位置,是否需要调用 requestLocationUpdates() ?

java - 在部署 Java Web 应用程序时如何知道 .properties 文件的位置以读取配置?

tomcat - keytool 错误 : java. lang.Exception:无法从回复建立链

java - fatal error :1:1: Content is not allowed in prolog. org.xml.sax.SAXParseException

java - Gmail REST API : 400 Bad Request + Failed Precondition

java - 如何自动增加字符串/整数以提高效率?

android - 设备重新启动后,地理围栏是否在 android 中保持 Activity 状态

android - 设置 Android 项目以使用 git

java - 用于删除 Java 应用程序当前文本字段中的条目的 SQL 命令