java吧 关注:1,236,822贴子:12,705,428
  • 3回复贴,共1

【求解答】将获取的用户名和密码存入数据库

只看楼主收藏回复

获取数据库,在main程序中能顺利执行并完成插入数据到数据库。可是把整个程序放在dopost中 就不能获取数据库了!
代码如下:
package com.hbsw;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Refund
*/
@WebServlet("/Refund")
public class Refund extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public Refund() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String username=request.getParameter("name");
String password=request.getParameter("psd");
System.out.println("用户名:"+username);
System.out.println("密码:"+password);
try {
Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序
//Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
Statement st = null;
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/MoBo","root","");
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
System.out.println("Success connect Mysql server!");
st = connect.createStatement();
String sql = "insert into refund(id,name,password) values(null,'"+username+"','"+password+"')";
//执行插入操作,executeUpdate方法返回成功的条数
int num = st.executeUpdate(sql);
if(num>0){
System.out.println("插入成功!!");
}
}catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}


IP属地:四川1楼2015-09-21 16:01回复
    错误信息:
    Error loading Mysql Driver!java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    .........
    ...........
    get data error!at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)


    IP属地:四川2楼2015-09-21 16:04
    回复


      IP属地:四川3楼2015-09-21 16:10
      回复
        加载JDBC驱动那行改成Class.forName("com.mysql.jdbc.Driver").newInstance();试试


        4楼2015-09-21 16:19
        回复