ajax包含mysql吗_ajax+mysql

public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

response.setContentType("text/html;charset=utf-8");

response.setCharacterEncoding("utf-8");

PrintWriter out=response.getWriter();

request.setCharacterEncoding("utf-8");

String id=request.getParameter("username");

String password=request.getParameter("password");

out.println("id = " +id);//Servlet操作数据库和普通java类一样

Connection ct= null;

PreparedStatement ps=null;

ResultSet rs= null;try{//1.加载驱动

Class.forName("com.mysql.jdbc.Driver");//2.得到连接

ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/work", "root", "1");//3.创建PreparedStatment 用于传送sql查询语句

ps=(PreparedStatement) ct.prepareStatement("select * from users where id =? and password=?");//给?赋值

ps.setObject(1, id);

ps.setObject(2, password);//4.执行操作

rs=ps.executeQuery();//5.根据结果做处理

if(rs.next())

{//合法

request.getRequestDispatcher("/MainFrame").forward(request, response);

}else{

request.setAttribute("error", "用户名 或者 密码错误!");

request.getRequestDispatcher("/LoginServlet").forward(request, response);

}

}catch(Exception e) {

e.printStackTrace();//TODO: handle exception

}finally{//关闭资源

if(rs!=null)

{try{

rs.close();

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

rs=null;

}if(ps!=null)

{try{

ps.close();

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

ps=null;

}if(ct!=null)

{try{

ct.close();

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

ct=null;

}

}//out.println("username"+username);

}public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {this.doGet(request, response);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值