Home > Back-end >  Why I can't insert a Dao data in a mysql?
Why I can't insert a Dao data in a mysql?

Time:09-25

Which big brother to help me see what is going wrong
BaseDao. Java:
Package com. Tao. Dao;

Import the Java. SQL. Connection;
Import the Java. SQL. DriverManager;
Import of Java, SQL PreparedStatement;
Import the Java. SQL. The ResultSet;
Import Java, SQL Statement;

Public class BaseDao {
Protected Connection conn=null;
Protected the Statement STMT=null;
Protected the 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 the connect () {
Try {
Class.forName("com.mysql.jdbc.Driver");
Conn=DriverManager. GetConnection (url, "root", "123456");
STMT=conn. CreateStatement ();
} the 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 ();
}
} the catch (Exception e) {

}
}
}

UserDao. Java:
Package com. Tao. Dao;

The import com. Tao. The entity. The User;

Public class UserDao extends BaseDao {
Public User the dologin (String name, String pass) {
The User u=null;
Try {
Super. The connect ();
String SQL="select * from the 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.s etId (rs. Get int (1));
U.s etName (rs. Get string (2));
U.s etPass (rs. Get string (3));

}
} the catch (Exception e) {
e.printStackTrace();
} the finally {
Super. CloseAll ();
}
The return of u;
}
Public int insert User (u) {
Int row=0;
Try {
Super. The connect ();
String SQL="insert into user (name, pass) values (?,?,?,?,? ,?) ";
PSTMT=conn. PrepareStatement (SQL);
PSTMT. SetString (1, u.g etName ());
PSTMT. SetString (2, u.g etPass ());
The row=PSTMT. ExecuteUpdate ();
} the catch (Exception e) {
e.printStackTrace();
} the finally {
Super. CloseAll ();
}
Return the row.
}
}

Doregister. JSP:
<% @ page import="com. Tao. Dao. UserDao" % & gt;
<% @ page import="com. Tao. The entity. The User" % & gt;
<% @ page language="Java" contentType="text/HTML. Charset=utf-8 "
PageEncoding="utf-8" % & gt;


<meta HTTP - equiv="content-type" Content="text/HTML. Charset=utf-8 "& gt;
Insert the title here

<body>
<%//to get name password, and age
String name=request. The getParameter (" username ");
String pass=request. The getParameter (" password ");
The User u=new User ();
U.s etName (name);
U.s etPass (pass);
UserDao udao=new UserDao ();
//call the method that udao implementation into function
Int x=udao. Insert (u);
If (x>=1) {
//forwarding, if & gt; 1, jump to success2. The JSP page
Request. GetRequestDispatcher ("/Success. JSP "). The forward (request, response);
}
% & gt;





Register. The JSP:
<% @ page language="Java" contentType="text/HTML. Charset=utf-8 "
PageEncoding="utf-8" % & gt;


<meta HTTP - equiv="content-type" Content="text/HTML. Charset=utf-8 "& gt;
Insert the title here

<body>

User name: & lt; Input type="text" name="username" & gt;
Password: & lt; Input type="text" name="password" & gt;





CodePudding user response:

https://blog.csdn.net/songleong/article/details/105968830

CodePudding user response:

The
refer to the original poster Qq0417ysq response:
which bosses to help me see what is going wrong



You this is mysql5 way to link, please make sure your mysql version is correct, the new version of the mysql link mode has been changed, please make your own baidu,

CodePudding user response:

An error message to send to
  • Related