哪個大佬幫我看看哪里出問題了
BaseDao.java:
package com.tao.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class BaseDao {
protected Connection conn = null;
protected Statement stmt = null;
protected ResultSet rs = null;
protected String url = "jdbc:mysql://localhost:3306/student3?characterEncoding=utf-8";
protected String name = "root";
protected String password = "123456";
protected PreparedStatement pstmt=null;
public void connect(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,"root","123456");
stmt = conn.createStatement();
} catch(Exception e) {
e.printStackTrace();
}
}
public void closeAll(){
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
if(pstmt!=null){
pstmt.close();
}
}catch(Exception e) {
}
}
}
UserDao.java:
package com.tao.dao;
import com.tao.entity.User;
public class UserDao extends BaseDao{
public User dologin(String name,String pass){
User u=null;
try {
super.connect();
String sql="select * from user where name=? and pass=? ";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setString(2,pass);
rs=pstmt.executeQuery();
while(rs.next()){
u=new User();
u.setId(rs.getInt(1));
u.setName(rs.getString(2));
u.setPass(rs.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
super.closeAll();
}
return u;
}
public int insert(User u){
int row=0;
try {
super.connect();
String sql="insert into user(name,pass) values(?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, u.getName());
pstmt.setString(2,u.getPass());
row=pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
super.closeAll();
}
return row;
}
}
Doregister.jsp:
<%@page import="com.tao.dao.UserDao"%>
<%@page import="com.tao.entity.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% //獲取姓名密碼和年齡
String name=request.getParameter("username");
String pass=request.getParameter("password");
User u=new User();
u.setName(name);
u.setPass(pass);
UserDao udao=new UserDao();
//呼叫udao的方法實作插入功能
int x=udao.insert(u);
if(x>=1){
//轉發,如果>1,跳轉到success2.jsp頁面
request.getRequestDispatcher("/Success.jsp").forward(request,response);
}
%>
</body>
</html>
Register.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="Doregister.jsp" method="post">
用戶名:<input type="text" name="username">
密碼:<input type="text" name="password">
<hr>
<input type="submit" value="https://bbs.csdn.net/topics/提交" >
<input type="reset">
</form>
</body>
</html>
uj5u.com熱心網友回復:
https://blog.csdn.net/songleong/article/details/105968830uj5u.com熱心網友回復:
你這個是mysql5的鏈接方式,請先確定你的mysql版本正確,新版本的mysql鏈接方式已經更改,請自行百度。
uj5u.com熱心網友回復:
報錯資訊給發出來轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71423.html
標籤:Web 開發
