June 2017 Short Semester Training Code Summary-----JDBC

Source: Internet
Author: User
Tags stub

This code mainly describes the local database, the use of the Java language for the increase and deletion, the specific code is as follows:

Interface Class Code

Package Cn.neusoft.mybatis.dao;

Import java.util.List;

Import Cn.neusoft.mybatis.pojo.Userinfo;

Public interface Userinfodao {

Inserting user information into the Userinfo entity class can carry user information
public boolean addUser (Userinfo Userinfo);
Modify user password based on user name
public boolean updateUser (Userinfo Userinfo);
Delete User information based on user name
public boolean deleteuser (String username);
Querying all user Information
Public list<userinfo> findAll ();
According to the user name to query personal information, according to the actual situation a user name will only find a user, so can only find records, so do not list
Public Userinfo GetUser (String username);
Public Userinfo Findbyusername (String username);
Choose to implement the following paging Query method
SELECT * FROM UserInfo limit pageSize (pageNo-1), pageSize;
Public list<userinfo> findbypage (int pagesize,int pageno);
}


Method Class Code:

Package Cn.neusoft.mybatis.dao;

Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.util.ArrayList;
Import java.util.List;

Import Cn.neusoft.mybatis.jdbc.Dbutils;
Import Cn.neusoft.mybatis.pojo.Userinfo;

public class Userinfodaoimpl implements Userinfodao {

@Override
public boolean addUser (Userinfo Userinfo) {
Reads the information from the UserInfo entity class, and then inserts the information into the database

1. Writing SQL statements
String sql = "INSERT into UserInfo (USERNAME,PWD) VALUES (?,?)";
2. If there is a question mark in the SQL statement, we need the corresponding information
The Userinfo.getusername () in the Obj object corresponds to the first question mark in this method
Userinfo.getpwd () In this method corresponds to a second question mark
object[] obj = new Object[]{userinfo.getusername (),
Userinfo.getpwd ()};
3. Execute the SQL statement and the Obj object into the Dbutils executeupdate method
Note that the Executeupdate method type of dbutils is of type int and can only be received using the INT type
int a = dbutils.executeupdate (sql, obj);
if (0!=a) {//If the insert operation can be executed, then the value of a will not be 0, then we think the operation is successful and returns true
return true;
}
return false;
}

@Override
public boolean updateUser (Userinfo Userinfo) {
TODO auto-generated Method Stub
String sql = "Update userinfo set pwd= '" +userinfo.getpwd () +
"' Where Username= '" +userinfo.getusername () + "'";
SYSTEM.OUT.PRINTLN (SQL);
Because we did not write the above SQL text? So do not use object[] objects for padding
int a = dbutils.executeupdate (sql, NULL);
if (0!=a) {
return true;
}
return false;
}
@Override
public boolean deleteuser (String username) {
TODO auto-generated Method Stub
String sql = "Delete from userinfo where username =?";
object[] obj = new Object[]{username};
int a = dbutils.executeupdate (sql, obj);
if (0!=a) {
return true;
}
return false;
}

Query all user information eventually returns a list collection
@Override
Public list<userinfo> FindAll () {
TODO auto-generated Method Stub
list<userinfo> list = new arraylist<userinfo> ();
String sql = "SELECT * from UserInfo";
Returns SQL query results to the ResultSet object
ResultSet rs = dbutils.executequery (sql, NULL);

try {
while (Rs.next ()) {
Because querying all information means that userinfo needs to be reused, it is used multiple times in the loop body
Userinfo
Userinfo Userinfo = new Userinfo ();
Put each set of information that is queried in the RS object into the UserInfo entity class
Userinfo.setuserid (Rs.getint ("userid"));
Userinfo.setusername (rs.getstring ("username"));
Userinfo.setpwd (rs.getstring ("pwd"));
List.add (userinfo);
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

return list;
}

@Override
Public Userinfo GetUser (String username) {
String sql= "SELECT * from UserInfo where username= '" +username+ "'";
ResultSet rs = dbutils.executequery (sql, NULL);
Userinfo Userinfo = new Userinfo ();
Put each set of information that is queried in the RS object into the UserInfo entity class

try {
if (Rs.next ()) {

Userinfo.setuserid (Rs.getint ("userid"));
Userinfo.setusername (rs.getstring ("username"));
Userinfo.setpwd (rs.getstring ("pwd"));

}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return userinfo;
}

@Override
Public Userinfo Findbyusername (String username) {
TODO auto-generated Method Stub
String sql= "SELECT * from UserInfo where username=?";
object[] obj = new Object[]{username};
ResultSet rs = dbutils.executequery (sql, obj);
Userinfo Userinfo = new Userinfo ();
try {
while (Rs.next ()) {
Because querying all information means that userinfo needs to be reused, it is used multiple times in the loop body
Userinfo
Put each set of information that is queried in the RS object into the UserInfo entity class
Userinfo.setuserid (Rs.getint ("userid"));
Userinfo.setusername (rs.getstring ("username"));
Userinfo.setpwd (rs.getstring ("pwd"));
}
}catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return userinfo;
}

@Override
Public list<userinfo> findbypage (int pageSize, int pageno) {
list<userinfo> list = new arraylist<userinfo> ();
int i=pagesize* (PAGENO-1);
String sql = "SELECT * from UserInfo limit?,?";
Returns SQL query results to the ResultSet object
object[] obj = new Object[]{i,pagesize};
ResultSet rs = dbutils.executequery (sql, obj);

try {
while (Rs.next ()) {
Because querying all information means that userinfo needs to be reused, it is used multiple times in the loop body
Userinfo
Userinfo Userinfo = new Userinfo ();
Put each set of information that is queried in the RS object into the UserInfo entity class
Userinfo.setuserid (Rs.getint ("userid"));
Userinfo.setusername (rs.getstring ("username"));
Userinfo.setpwd (rs.getstring ("pwd"));
List.add (userinfo);
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

return list;
}
}


Test method Code:

Package cn.neusoft.mybatis.test;

Import static org.junit.assert.*;

Import java.util.List;

Import Org.junit.Test;

Import Cn.neusoft.mybatis.dao.UserinfoDao;
Import Cn.neusoft.mybatis.dao.UserinfoDaoImpl;
Import Cn.neusoft.mybatis.pojo.Userinfo;

public class Userinfodaoimpltest {

Private Userinfodao Userinfodao = new Userinfodaoimpl ();
/* @Test
public void Testadduser () {
Load a class
Userinfodao dao = new Userinfodaoimpl ();
Put the information you get on a webpage into the Userinfo entity class
Userinfo Userinfo = new Userinfo ("Moucong", "123");
Boolean B = Dao.adduser (userinfo);
if (b) {
SYSTEM.OUT.PRINTLN ("Registered successfully! ");
}else{
SYSTEM.OUT.PRINTLN ("Registration failed! ");
}
}

@Test
public void Testupdateuser () {
Userinfodao dao = new Userinfodaoimpl ();
Userinfo Userinfo = new Userinfo ("Moucong", "0627");
Boolean B = Dao.updateuser (userinfo);
if (b) {
SYSTEM.OUT.PRINTLN ("Update succeeded! ");
}else{
System.out.println ("Update failed! ");
}
}

@Test
public void Testdeleteuser () {
Boolean B = Userinfodao.deleteuser ("Moucong");
System.out.println (b);
}

@Test
public void FindAll () {
list<userinfo> list = Userinfodao.findall ();
Print the user name from the set of data in the first position of the list
System.out.println (List.get (0). GetUserName ());
Loop the way to print all user information
for (int i =0;i<list.size (); i++) {
System.out.println ("~~~~~~~~~~~~~~~~~~~~");
SYSTEM.OUT.PRINTLN ("User number:" +list.get (i). GetUserid ());
System.out.println ("User name:" +list.get (i). GetUserName ());
SYSTEM.OUT.PRINTLN ("User password:" +list.get (i). GETPWD ());
}
}

@Test
public void GetUser () {
Userinfo list = Userinfodao.getuser ("Moucong");
SYSTEM.OUT.PRINTLN ("User number:" +list.getuserid ());
System.out.println ("User name:" +list.getusername ());
SYSTEM.OUT.PRINTLN ("User password:" +list.getpwd ());
}*/
@Test
public void Findbypage () {
List<userinfo> List=userinfodao.findbypage (2, 2);
for (int i =0;i<list.size (); i++) {
System.out.println ("~~~~~~~~~~~~~~~~~~~~");
SYSTEM.OUT.PRINTLN ("User number:" +list.get (i). GetUserid ());
System.out.println ("User name:" +list.get (i). GetUserName ());
SYSTEM.OUT.PRINTLN ("User password:" +list.get (i). GETPWD ());
}
}


}

June 2017 Short Semester Training Code Summary-----JDBC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.