我是 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);
}
}
最佳答案
步骤是:
- 加载合适的驱动(驱动类放在WEB-INF/lib文件夹下)
- 创建一个
Connection
对象。 - 执行
语句
。 - 遍历
ResultSet
。 - 获取列中特定行的值。
现在,看看您的代码:
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
}
您可以使用以下任一方法:
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, ...
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/