package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.util.JdbcUtil;
public class Ajax extends HttpServlet {
private static final long serialVersionUID = 1L;
public Ajax() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");response.setContentType("text/html");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
PrintWriter out = response.getWriter();
String uname = request.getParameter("uname");
try {
conn = JdbcUtil.getConnection();
String sql = "select * from user_1 where uname=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, uname);
rs = pstmt.executeQuery();
if(rs.next()){
System.out.println("json");
// String json="{\"userName\":\""+uname+"\",\"userNo\":\"123\"}";
String json="{\"userName\":\""+uname+"\",\"userNo\":123}";
// String json="{userName:"+uname+",userNo:123}";
out.write(json);
System.out.println(json);
}else{out.print("false");
System.out.println(uname);}
out.close();
JdbcUtil.closeConnection(conn, pstmt);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}