<> utilize Map Gather, store and print database information

java Exercises

Purpose of the experiment :

1, Creating learning hash map ;

2, Master use HashMap To create a key - Value pair ;

3, Master the key to find the corresponding value .

Experimental requirements :

Task 1 :
Write a Java application program , Complete the following functions .
(1) to write Student class , Include name , Student number , Gender and student identity ( Identity can be a student , monitor , Academic Committee , Deputy monitor ).

(2) Define a map Gather and store a group of student information , Key value is student number ,value by Student object .

Ergodic set , Output all keys in collection - Value pair .

(3) Submit source and run screenshots .
package Test; import java.util.HashMap; import java.util.Map; import java.util.
Scanner; import java.util.Set; import Dao.StudentDaoImpl; import Util.Student;
import Util.StudentDao; // Test class public class TestMap { public static void main(
String[] args) { String classid = null; Scanner reader = new Scanner(System.in);
System.out.println(" Please enter the class number :"); classid = reader.next(); Map<String,Student> map
= new HashMap<String,Student>(); StudentDao s = new StudentDaoImpl(); map = s.
selectAllbyMap(classid); // If returned Map Set size is 0 if(map.size() != 0){ System.out.
println(" Key value pair traversal "); Set<Map.Entry<String, Student>> set = map.entrySet(); for(Map.
Entry<String, Student> entry : set){ String key = entry.getKey(); Student value
= entry.getValue(); System.out.println(key + "-" + value); } }else System.out.
println(" Information not found "); reader.close(); } } package Dao; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.
SQLException; import java.sql.Statement; import java.util.ArrayList; import java
.util.HashMap; import java.util.List; import java.util.Map; import Util.
DBConnection; import Util.Student; import Util.StudentDao; // Inheritance class inherit StudentDao Interface
public class StudentDaoImpl implements StudentDao{ Connection conn = null;
// Construction method Automatically connect to database on call public StudentDaoImpl(){ DBConnection db = new DBConnection();
conn= db.getConn(); } public Map<String,Student> selectAllbyMap(String classid)
{ Map<String,Student> map = new HashMap<String,Student>(); try { String sql =
null; sql = "select id,sname,ssex,sage,classid from student where classid = ?";
// Pretreatment PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1,
classid); pst.execute(); ResultSet re = pst.executeQuery(); while(re.next()){
Student c= new Student(); c.setId(re.getString(1)); c.setName(re.getString(2));
c.setSex(re.getString(3)); c.setAge(re.getInt(4)); c.setClassid(re.getString(5))
; String id = re.getString(1); map.put(id,c); } // close database pst.close(); conn.close(
); } catch (SQLException e) { // TODO Auto-generated catch block e.
printStackTrace(); } return map; } // This method was written before With List aggregate This program did not call public List<
Student> selectAll(String classid) { List<Student> list = new ArrayList<Student>
(); try (Statement stmt = conn.createStatement();){ String sql = null; sql =
"select * from student where classid =" + classid; ResultSet re = stmt.
executeQuery(sql); while(re.next()){ Student c = new Student(); c.setId(re.
getString(1)); c.setName(re.getString(2)); c.setSex(re.getString(3)); c.setAge(
re.getInt(4)); c.setClassid(re.getString(5)); list.add(c); } stmt.close(); conn.
close(); } catch (SQLException e) { // TODO Auto-generated catch block e.
printStackTrace(); } return list; } } package Util; import java.util.List;
import java.util.Map; // Interface public interface StudentDao { public List<Student>
selectAll(String classid); public Map<String,Student> selectAllbyMap(String
classid); } package Util; //Student class public class Student { String id; String
name; String sex; int age; String classid; // encapsulation public String getId() { return
id; } public void setId(String id) { this.id = id; } public String getName() {
return name; } public void setName(String name) { this.name = name; } public
StringgetSex() { return sex; } public void setSex(String sex) { this.sex = sex;
} public int getAge() { return age; } public void setAge(int age) { this.age =
age; } public String getClassid() { return classid; } public void setClassid(
String classid) { this.classid = classid; } // Nonparametric construction method public Student() { super();
} // Parametric construction method public Student(String id, String name, String sex, int age, String
classid) { super(); this.id = id; this.name = name; this.sex = sex; this.age =
age; this.classid = classid; } // rewrite toString method public String toString() { return
" Student number :" + id + " full name :" + name + " Gender :" + sex + " Age :" + age + " Class number :" + classid; }
} package Util; import java.sql.Connection; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.Statement; public class
DBConnection{ //JDBC connect mysql String url = "jdbc:mysql://localhost:3306/school";
String user= "root"; String password = "418521"; Connection conn = null;
Statement stmt= null; public Connection getConn(){ try { Class.forName(
"com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url,user,password);
stmt= conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); }
catch (ClassNotFoundException e) { e.printStackTrace(); } return conn; } }
Here are the results :

Enter a class number that does not exist in the database Information not found

query was successful !

<> Knowledge points

Map Set characteristics
Store any key value pair (key-value), key : disorder , No subscript , Key content is not repeatable ; value : disorder , nothing subscript , Values can be repeated .
Map Implementation class of collection :hashMap

Key value pair traversal
Treat all key value pairs as a whole Map.Entry, because key and value Characteristics of ,key- value The whole is
disorder , No subscript , Element content is not repeatable , So it's a Set aggregate . adopt entrySet() Method returns all
Key value pair Set aggregate . Pass again getKey() and getValue() Separately obtained key and value
// Key value pair traversal System.out.println("----- Key value pair traversal ----"); Set<Map.Entry<String,String>> set
= map.entrySet(); for (Map.Entry<String, String> entry : set) { String key =
entry.getKey(); String value = entry.getValue(); System.out.println(key+"-"+
value); }
My computer sophomore , I want to organize every assignment into an article and share it with you .
If there's something wrong with the program , I hope you can point out , Learn and improve with you !

Technology
©2019-2020 Toolsou All rights reserved,
CSS architecture design NOI2019 travels Detailed explanation ubuntu14.04 How to set static IP2019 Year end summary —— Second year of work Big data tells you , How tired are Chinese women java Simple lottery algorithm , luck draw DemoPYTHON Summary of final review Classical algorithm - recursion ( The case of raw rabbit )2021 year 1 Monthly programmer salary statistics , average 14915 element First knowledge python Skills summary