关于servlet中调用数据库连接的问题,对数据库的操作老是无效,求高手指点
我写了三个类,一个是customer类,一个是Register类(专门处理数据连接),一个是Check_validate类,用来判断一些逻辑,也引用了Register的方法
但是在Check_validate中的操作在数据库中一点反应也没有,求高手指点
package BasementClass;
public class Customer{
private String Username;
private String Password;
private String Name;
private String Address;
private String E_mail;
public Customer(String Username,String Password,String Name,String Address,String E_mail){
this.Username=Username;
this.Password=Password;
this.Name=Name;
this.Address=Address;
this.E_mail=E_mail;
}
public String getUsername(){
return Username;
}
public String getPassword(){
return Password;
}
public String getName(){
return Name;
}
public String getAddress(){
return Address;
}
public String getE_mail(){
return E_mail;
}
public void setUsername(String username){
Username=username;
}
public void setPassword(String password){
Password=password;
}
public void setName(String name){
Name=name;
}
public void setAddress(String address){
Address=address;
}
public void setE_mail(String e_mail){
E_mail=e_mail;
}
}
package Site_SQL;
import BasementClass.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Check_validate extends HttpServlet {
String user_name,pwd,re_pwd,address,name,email,note;
Register r;
Customer c;
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
String docType =
" <!DOCTYPE HTML PUBLIC \ "-//W3C//DTD HTML 4.0 "+ "Transitional//EN\ "> \n ";
response.setContentType( "text/html ");
response.setCharacterEncoding( "UTF-8 ");
PrintWriter out = response.getWriter();
user_name=request.getParameter( "user ");
pwd=request.getParameter( "pwd ");
re_pwd=request.getParameter( "re_pwd ");
address=request.getParameter( "address ");
email=request.getParameter( "mail ");
name=request.getParameter( "name ");
boolean flag=true;
if(user_name.equals( " ")||user_name==null
||pwd.equals( " ")
||pwd==null
||address.equals( " ")
||address==null
||email.equals( " ")
||email==null
||name.equals( " ")
||name==null){
note= "您有必填数据项未填写 ";
flag=false;
}
if(!pwd.equals(re_pwd)){
note= "password error ";
flag=false;
}
if(r.CheckDiffer(user_name)){
note= "the user is already exist ";
flag=false;
}
if(flag){
c=new Customer(user_name,pwd,name,address,email);
r.InsertRow(c);
out.print(docType+ " <html> <head> <title> 成功 </title> <body> "+
"regster success "+ " </body> </html> ");
r.destroy();
}
else{
out.print(docType+ " <html> <head> <title> 错误 </title> <body> "+user_name+
note+ " <form name=\ "form\ " method=\ "get\ " action=\ "\\ShoeSiteWeb\\register_deal.html\ "> "
+ " <input type=\ "submit\ " name=\ "submit\ " value=\ "重写\ "> "+ " </body> </html> ");
r.destroy();
}
}
}
package Site_SQL;
import java.sql.*;
import BasementClass.*;
public class Register{
public String getQuery;
String url= "jdbc:odbc:ShoeSite_info ";
Connection con=null;
String sql,insert,delete,update;
PreparedStatement st=null;
ResultSet rs=null;
Statement str=null;
public Register(){
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
}catch(java.lang.ClassNotFoundException e){
System.err.print( "class not found ");
}
try{
con=DriverManager.getConnection(url, " ", " ");
}catch(Exception e){
System.out.println( "exceptions "+e);
}
}
public boolean CheckDiffer(String username){
boolean flag=false;
try{
str=con.createStatement();
sql= "SELECT username from customermessage ";
rs=str.executeQuery(sql);
while(rs.next()){
getQuery=rs.getString(1);
if(getQuery.equals(username.trim()))
{
flag=true;
break;
}
}
}catch(Exception e){
System.out.println( "exceptions "+e);
}
return flag;
}
public void InsertRow(Customer ct){
try{
str=con.createStatement();
insert= "INSERT INTO customermessage VALUES( ' "+ct.getUsername()+ " ', ' "+ct.getPassword()+ " ', ' "+ct.getName()+ " ', ' "+ct.getAddress()+ " ', ' "+ct.getE_mail()+ " ') ";
str.executeUpdate(insert);
}catch(Exception e){
System.out.println( "exceptions "+e);
}
}
public void destroy(){
try{
str.close();
con.close();
}catch(Exception ioe){
System.out.println( "exceptions3 ");
}
}
/*public static void main(String[]args){
Register r=new Register();
boolean b;
//b=r.CheckDiffer( "yyyy ");
if(r.CheckDiffer( "yy "));
//System.out.print( "存在 ");
//else
//System.out.print( "不存在 ");
// Customer c=new Customer( "ls ", "346534 ", "dsfs ", "dfsg ", "sdfsf ");
// r.InsertRow(c);
r.destroy();
}
*/
}
[解决办法]
con=DriverManager.getConnection(url, " ", " ");
这里应该有问题 你进入数据库的用户名是什么啊~~~密码是什么。。。难道都是空嘛~~
[解决办法]
是不是配置的问题servlet需要在web.xml里面配置
[解决办法]
user_name=request.getParameter( "user ");
pwd=request.getParameter( "pwd ");
re_pwd=request.getParameter( "re_pwd ");
address=request.getParameter( "address ");
email=request.getParameter( "mail ");
name=request.getParameter( "name ");
你这些数据获取是空,你是通过连接数据库获得这些数据,并不是通过请求获取的.
[解决办法]
用hibernate吧!
[解决办法]
user_name=request.getParameter( "user ");
pwd=request.getParameter( "pwd ");
re_pwd=request.getParameter( "re_pwd ");
address=request.getParameter( "address ");
email=request.getParameter( "mail ");
name=request.getParameter( "name ");
这些是通过表单提交来获得的 你没有表单呀