第一个java文件 DBManager.java
package com.book.util;import java.sql.*;
/*
* 实现数据库的连接
*/
public class DBManager {
public static Connection getConnection()
{
Connection con=null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root","12315");
}catch(Exception e)
{
e.printStackTrace();
}
return con;
}
}
package com.book.util;import java.sql.*;
/*
* 实现数据库的连接
*/
public class DBManager {
public static Connection getConnection()
{
Connection con=null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root","12315");
}catch(Exception e)
{
e.printStackTrace();
}
return con;
}
}
import com.book.util.DBManager;
import java.sql.*;public class UserDBManager {
public UserDBManager()
{
}
private static final String sql="selct username,password from user where username=? and password=?";
public boolean checkDB(User u)
{
boolean b=false;
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
try
{
con=DBManager.getConnection();//这儿错了,为什么还是null
pst=con.prepareStatement(sql);
pst.setString(1, u.getUsername());
pst.setString(2, u.getPassword());
rs=pst.executeQuery();
if(rs.next())
b=true;
}catch(SQLException e)
{
e.printStackTrace();
}finally
{
if(con!=null)
{
try
{
con.close();
}catch(SQLException e)
{
e.printStackTrace();
}
}
}
return b;
}
/*
public User checkCB(User u)
{
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
User temp=new User();
try
{
con=DBManager.getConnection();
pst=con.prepareStatement(sql);
pst.setString(1, u.getUsername());
pst.setString(2, u.getPassword());
rs=pst.executeQuery();
if(rs.next())
{
temp.setUsername(rs.getString(1));
temp.setPassword(rs.getString(2));
}
}catch(SQLException e)
{
e.printStackTrace();
}finally
{
if(con!=null)
{
try
{
con.close();
}catch(SQLException e)
{
e.printStackTrace();
}
}
}
return temp;
}
*/
}
package com.book.domain;
public class User {
private String username="";
private String password="";
public User()
{
}
public User(String username,String password)
{
this.username=username;
this.password=password;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username=username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}}
package com.book.web;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.book.domain.User;
import com.book.util.UserDBManager;public class GetInfoServlet extends HttpServlet { /**
* Constructor of the object.
*/
public GetInfoServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
String username=request.getParameter("username");
String password=request.getParameter("password");
User u=new User();
u.setUsername(username);
u.setPassword(password);
UserDBManager udb=new UserDBManager();
boolean a=udb.checkDB(u);
if(a)
{
request.getRequestDispatcher("book.jsp").forward(request, response);
}
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}}
<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>图书管理系统</title>
<link rel="stylesheet" type="text/css" href="css/index.css">
</head>
<body>
<div id="total">
<div id="header" > </div>
<div id="main">
<div id="main-top">
</div>
<div id="loginForm">
<form action="servlet/GetInfoServlet" method="post" onsubmit="return LoginSubmit()">
用户名:<input type="text" name="username" maxlength="20"><br>
密   码: <input type="password" name="password" maxlength="20"><br>
<input type="submit" value="登录" name="submit">     <input type="reset" value="取消" name="cancle">
</form>
</div>
</div>
<div id="footer"></div>
</div>
</body>
<html>
既然是这里错了。 那应该找这个类DBManager,继而找
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB","root","12315");
看下连接是否错误 。用Java连接mysql的我没用过 。asp的连接MySQL的熟悉过一段时间 。,楼主去网上查查吧!然后把异常信息贴出来吧!
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'selct username,password from user where username='jacky' and password='123156'' at line 1其中 那个username='jacky' 和那个密码 password='123156' 是我在index.jsp 登入时输入的账号密码~ 好像是我的sql写错了?是不是这样啊...
这都看不懂么?
楼上的已经说了,不过可以理解,LZ自己写的SQL语句都没觉得有问题,更不会仔细看异常信息了,LZ还是心放细点吧,还贴了这么多代码,切入问题的关键点才是最重要的,还好没有慢慢看全部的代码。
常用的调试方法是将完整的sql语句打印出来,然后查看打印的sql语句是否有明显的错误,如果sql语句比较复杂,发现不了,就在数据库自带的查询工具里面执行这条sql语句,那里面的错误信息会提示得更详细。
想你这个错误,明显是没有细心去找的:
selct username,password from user where username='jacky' and password='123156'
第一个关键字selct应该拼成select。
新手做程序,多一点细心和独立,会成长得更快些!加油!