server端连接database总有问题
MyEclipse+MySQL+Tomcat
想建立一个ConnectionManager用来连接database,可以用于每个功能
database中有一个table:account(username,password,tele,email)
ConnectionManager.java如下
package com.lu.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectionManager {
private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/grocery?useUnicode=true&characterEncoding=utf8";
private static final String DATABASE_USERNAME = "root";
private static final String DATEBASE_PASSWORD = "0000";
// 获取连接
public static Connection getConnection() throws SQLException {
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = DriverManager.getConnection(DATABASE_URL,
DATABASE_USERNAME, DATEBASE_PASSWORD);
return con;
}
public static void main(String[] args) {
try {
Connection con=getConnection();
Statement stmt=con.createStatement();
stmt.execute("select account(username,password) values('wangwu','123')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
LoginServlet.java如下:
package com.lu.servlet;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lu.db.ConnectionManager;
import com.lu.model.User;
@SuppressWarnings("unused")
public class LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/html;charset=gb2312");
// 取出表单中用户输入的用户名
String username = request.getParameter("username");
System.out.println("=================>"+username);
// 取出表单中用户输入的密码
System.out.println("u能传过来吗?"+username);
String password = request.getParameter("password");
System.out.println("p能传过来吗?"+password);
PrintWriter pw=resp.getWriter();
//OutputStream out=resp.getOutputStream();
// 创建出用户
User user = new User(username, password);
if (isExist(user)) {
// 为真 说明用户存在
request.setAttribute("username", username);
resp.sendRedirect("success.jsp");
//req.setAttribute("lab", "true");
pw.println(true);
//out.write(1);
} else {
resp.sendRedirect("error.jsp");
pw.println(false);
//out.write(2);
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter pw=resp.getWriter();
String username = req.getParameter("username");
System.out.println("u能传过来吗?"+username);
String password = req.getParameter("password");
System.out.println("p能传过来吗?"+password);
User user = new User(username, password);
if (isExist(user)) {
// 为真 说明用户存在
req.setAttribute("username", username);
//resp.sendRedirect("success.jsp");
//req.getRequestDispatcher("success.jsp").forward(req, resp);
String sql="select * from account;";
int count=0;
List<User> list=new ArrayList<User>();
try {
Connection con=ConnectionManager.getConnection();
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
String username1=rs.getString("username");
String password1=rs.getString("password");
User user1=new User(username1,password1);
list.add(user1);
}
//获得所有用户的数量
count=list.size();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pw.println(count);
pw.println(true);
System.out.println("===pw==="+pw);
} else {
//resp.sendRedirect("error.jsp");
pw.println(false);
}
}
// 判断用户输入的用户名密码是否在正确
private boolean isExist(User user) {
// 用户名存在
try {
Connection con = ConnectionManager.getConnection();
String sql = "select * from account";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String username = rs.getString("username");
String password = rs.getString("password");
if (user.getUsername().equals(username)
&& user.getPassword().equals(password)) {
// 用户登录用户名密码 一致
System.out.println(username);
System.out.println(password);
return true;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
[解决办法]
??错呢。连接字符串打印出来对比下看对不对