Home > Back-end >  The idea of SQL server2008 database connection can't insert the data
The idea of SQL server2008 database connection can't insert the data

Time:09-21

Package com. Gyx. Db.

The import com. Gyx. Beans. Tuser;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Import the Java. SQL. SQLException;

Public class PreparedStatementUserSQL {
Public int insertUserTest (Tuser Tuser) {
Connection conn=null;
PreparedStatement PSTMT=null;
int num=0;
Try {
Conn=SQLJDBConnection. GetConn ();
String SQL="insert tuser (id, name, PWD, sex, age, phone) values (?,?,?,?,? ,? ,? ,? ,? ,?) ";
PSTMT=conn. PrepareStatement (SQL);
PSTMT. SetString (1, tuser getId ());
PSTMT. SetString (2, tuser getName ());
PSTMT. SetString (3, tuser getPwd ());
PSTMT. SetString (4, tuser getSex ());
PSTMT. SetInt (5, tuser getAge ());
PSTMT. SetString (6, tuser getPhone ());
Num=PSTMT. ExecuteUpdate ();
} the catch (SQLException e) {
e.printStackTrace();
} the catch (a ClassNotFoundException e) {
e.printStackTrace(); }
return num;
}
Private void closeAll (Connection conn, PreparedStatement PSTMT, Object o {
}

Public int updateTest (Tuser Tuser) {
Connection conn=null;
PreparedStatement PSTMT=null;
int num=0;
Try

{
Conn=SQLJDBConnection. GetConn ();
String SQL="update tuser set name=? , the age=? , where id=? ";
PSTMT=conn. PrepareStatement (SQL);
PSTMT. SetString (1, tuser getName ());
PSTMT. SetInt (2, tuser getAge ());
PSTMT. SetString (3, tuser getId ());
Num=PSTMT. ExecuteUpdate ();
System. The out. Println (num);
} the catch (SQLException e) {
e.printStackTrace();
} the catch (a ClassNotFoundException e) {
e.printStackTrace();

}
return num;
}

Public int deleteTest (Tuser Tuser) {
The Connection conn=null;
PreparedStatement PSTMT=null;
Int num=0;
Try {
Conn=SQLJDBConnection. GetConn ();
String SQL="delete from tuser where id=? ";
PSTMT=conn. PrepareStatement (SQL);
PSTMT. SetString (1, tuser getId ());
Num=PSTMT. ExecuteUpdate ();
System.out.println(num);
} the catch (SQLException e) {
e.printStackTrace();
} the catch (a ClassNotFoundException e) {
e.printStackTrace();
} the finally {
CloseAll (conn, PSTMT, null);//release resources
}
return num;
}

Public ResultSet selectTest (Tuser Tuser) {
The Connection conn=null;
PreparedStatement PSTMT=null;
The ResultSet ResultSet=null;
Try {
Conn=SQLJDBConnection. GetConn ();
String SQL="select * from tuser where id=? ";
PSTMT=conn. PrepareStatement (SQL);
PSTMT. SetString (1, tuser getId ());
The resultSet=PSTMT. ExecuteQuery ();

} the catch (SQLException e) {
e.printStackTrace();
} the catch (a ClassNotFoundException e) {
e.printStackTrace();
}
Return a resultSet.
}

}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Package com. Gyx. Bean;

Public class Tuser {
String id, name, PWD, sex, phone;
int age;

Public String getId () {
return id;
}

Public void setId (String id) {
this.id=id;
}

Public String getName () {
return name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}

Public String getPwd () {
return pwd;
}

Public void setPwd (String PWD) {
Enclosing the PWD=PWD;
}

Public String getSex () {
Return sex;
}

Public void setSex (String sex) {
this.sex=sex;
}

Public String getPhone () {
Return the phone;
}

Public void setPhone (String phone) {
This. The phone=phone;
}

Public int getAge () {
return age;
}

Public void setAge (int age) {
this.age=age;
}
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Package com. Gyx. Servlet.

The import com. Gyx. Beans. Tuser;
The import com. Gyx. Db. PreparedStatementUserSQL;

import javax.servlet.ServletException;
The import javax.mail. Servlet. The annotation. WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ WebServlet (name="InsertServlet," value="https://bbs.csdn.net/insertuser")
Public class InsertServlet extends the HttpServlet {
Protected void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);

}

Protected void doGet (it request, HttpServletResponse response) throws ServletException, IOException {
Tuser Tuser=new Tuser ();
Tuser. SetId (request. The getParameter (" id "));
Tuser. Elegantly-named setName (request. The getParameter (" name "));
Tuser. SetPwd (request. The getParameter (" PWD "));
Tuser. SetSex (request. The getParameter (" sex ")); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related