packagecom.lyq;importjava.io.PrintWriter;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statem...
package com.lyq;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 3401372898610232752L;
public void doGet(HttpServletRequest req, HttpServletResponse resp){
String url = "jdbc:mysql://localhost:3306/test";
Connection con = null;
Statement stat = null;
ResultSet rs = null;
Boolean bool;
resp.setContentType("text/html;charset=gbk"); // 为解决打印出来的中文乱码
try{
PrintWriter pw = resp.getWriter();
try{
Class.forName("com.mysql.jdbc.Driver");
pw.println("mysql 驱动程序已加载 ...");
}
catch (Exception ex){
pw.println("无法加载mysql驱动程序" + ex.getMessage());
return;
}
try{
con = DriverManager.getConnection(url, "root", "qinliang");
stat = con.createStatement();
String sqlString = "select * from tb";
rs = stat.executeQuery(sqlString);
while (rs.next()){
String name = rs.getString("username");
String mima = rs.getString("password");
System.out.println(name);
}
if(true){
resp.sendRedirect(req.getContextPath()+"/1/success.jsp");
}
rs.close();
stat.close();
con.close();
}
catch (SQLException ex){
while (ex != null){
ex.printStackTrace(pw);
ex = ex.getNextException();
}
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
为什么在while{}外边name无法解析为变量
展开