1. Gradually establish and build institutions according to the requirements of the javaweb project. The specific class packages include: model, db, dao, test;
For details of the specific architecture, please refer to the figure below:
2. Create a new database test and database table t_userinfo based on the project structure and add the corresponding test data; (I am using the green version of the database here, the specific download address: http://pan.baidu.com/s/1mg88YAc)
For details on the specific database establishment operation, please refer to the figure below:
3. Write various types of codes in the package, and the specific reference codes are as follows:
UserInfo.java
/** * FileName: UserInfo.java * @Description: TODO encapsulated object information* Copyright: person * Company person * @author: gaoxing * @version V1.0 * Createdate: 2014-5-25 2:26:41 pm * * Modification History: * Date Author Version Discription * --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- is modified: <Description of the modification reason> */package com.org.user.model; /** * @ClassName: UserInfo * @Description: TODO encapsulate the information of the object* @author: gaoxing * @date: 2014-5-25 2:26:41 pm */public class UserInfo { private int userid; private String username; private String password; /** * @Title: UserInfo * @Description: TODO (describe the function of this method) * @param: @param userid * @param: @param username * @param: @param password * @throws */ public UserInfo(int userid, String username, String password) { super(); this.userid = userid; this.username = username; this.password = password; } /** * @Title: UserInfo * @Description: TODO parameterless construction method* @param: * @throws */public UserInfo() { super();} public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } 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; } }UserInfoDBManger.java
/** * FileName: UserInfoDBManger.java * @Description: TODO Operation to connect to the database* Copyright: person * Company person * @author: gaoxing * @version V1.0 * Createdate: 2014-5-25 2:47:38 pm * * Modification History: * Date Author Version Discription * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ & What is modified: <Description of the modification reason> */package com.org.user.db; import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException; import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement; /** * @ClassName: UserInfoDBManger * @Description: TODO's operation to connect to the database* @author: gaoxing * @date: 2014-5-25 2:47:38 pm */public class UserInfoDBManger { private static Connection conn = null; private PreparedStatement ps = null; private ResultSet rs = null; public static Connection getConn() { String url = "jdbc:mysql://localhost:3306/test"; try { Class.forName("com.mysql.jdbc.Driver"); try { conn = (Connection) DriverManager.getConnection(url, "root", "mysql"); } catch (SQLException e) { System.out.println(e.getMessage()); } } catch (ClassNotFoundException e) { System.out.println(e.getMessage()); } return conn; } public void close() { try { rs.close(); ps.close(); conn.close(); } catch (SQLException e) { e.getMessage(); } } }UserInfoDao.java
/** * FileName: UserInfoDao.java * @Description: TODO Processes object information for operation through database connection* Copyright: person * Company person * @author: gaoxing * @version V1.0 * Createdate: 2014-5-25 2:36:09 pm * * Modification History: * Date Author Version Discription * --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Why & What is modified: <Description of the modification reason> */package com.org.user.dao; import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement;import com.org.user.db.UserInfoDBManger;import com.org.user.model.UserInfo; /** * @ClassName: UserInfoDao * @Description:TODO handles the operation object information through the database connection* @author: gaoxing * @date: 2014-5-25 2:36:09 pm */public class UserInfoDao { Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; public List<UserInfo> find(){ List<UserInfo> list=new ArrayList<UserInfo>(); String sql="select * from t_userinfo "; conn=UserInfoDBManger.getConn(); try { ps=(PreparedStatement) conn.prepareStatement(sql); rs=ps.executeQuery(); while (rs.next()) { UserInfo ui=new UserInfo(); ui.setUserid(rs.getInt(1)); ui.setUsername(rs.getString(2)); ui.setPassword(rs.getString(3)); list.add(ui); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }}UserInfoTest.java
/** * FileName: UserInfoTest.java * @Description: TODO method to test dao package* Copyright: person * Company person * @author: gaoxing * @version V1.0 * Createdate: 2014-5-25 5:43:03 pm * * Modification History: * Date Author Version Discription * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- What is modified: <Description of the modification reason> */package com.org.user.test; import static org.junit.Assert.*; import java.util.List; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.org.user.dao.UserInfoDao; import com.org.user.model.UserInfo; /** * @ClassName: UserInfoTest * @Description:TODO Method for testing dao packages* @author: gaoxing * @date: 2014-5-25 5:43:03 pm */public class UserInfoTest { /** * @Title: setUpBeforeClass * @Description: TODO (describe the function of this method) * @param: @throws java.lang.Exception * @return: void * @throws */ @BeforeClass public static void setUpBeforeClass() throws Exception { } /** * @Title: tearDownAfterClass * @Description: TODO (describe the function of this method) * @param: @throws java.lang.Exception * @return: void * @throws */ @AfterClass public static void teaDownAfterClass() throws Exception { } /** * Test method for {@link com.org.user.dao.UserInfoDao#find()}. */ @Test public void testFind() { UserInfoDao udao=new UserInfoDao(); List<UserInfo> list=udao.find(); for (int i = 0; i < list.size(); i++) { UserInfo ui=list.get(i); System.out.println("name: "+ui.getUsername()+"Password: "+ui.getPassword()); } } }4. After writing the class content, the auxiliary JUnit test package junit.jar should be added during the project construction process, and the database connection mysq-connector-java-5.1.7-bin.jar sqljdbc.jar should be imported, so that I can connect to the database;
5. After all the work is done, you can start the service and run it to view the results. If the following results appear on the JUnit test bench and console, it means that the project has been run successfully.
The above is the entire content of the example code that the editor brings to you for connecting to the unit test of mysql database to query data. I hope everyone supports Wulin.com more~