这次的设计也算是失败了百分之40的,不过拿来练手也确实是学到了很多东西,学会了解决问题的很多不通的方法,整个系统是一个子集而已,还需要很多完善,而且想做一个高仿的,看来还是需要继续改进,最近又要忙了,自己给自己打气,晓儿,你加油哦!
//°ÑžÃÀàÉèÖóɵ¥ÀûÀ࣬ÌṩÕûžö³ÌÐòµÄºËÐÄÂߌœá¹¹
public class MrpCore{
private ArrayList<Student> loadFromDB = new ArrayList<Student>();
private String username = "root";
private String password = "leon94941";
private String host = "jdbc:mysql://127.0.0.1:3306/school";
//Connection conn = null;
// Statement state = null ;
// ResultSet rs = null;
//ListIterator<Student> indexItem = loadFromDB.listIterator();
private ListIterator<Student> indexItem;
//ÓÃÓÚË÷ÒýÈ«ŸÖµÄÊýŸÝËùÔÚµÄλÖÃ
private static MrpCore instance = null ;
public static MrpCore getInstance(){
if(instance == null)
instance = new MrpCore();
return instance;
}
public void writeToDB(){
ResultSet result = null;
try{
Connection conn = DriverManager.getConnection(host,username,password);
Statement state = conn.createStatement();
//ÖŽÐвÙ×÷µÄsqlÓïŸä
String sql = "delete from student";
state.execute(sql);
Iterator<Student> it = loadFromDB.iterator();
while(it.hasNext()){
state.clearBatch();//Çå¿Õµ±Ç°µÄÃüÁîÁÐ±í£»
Student temp = it.next();
String name = temp.getName();
String age = temp.getAge();
String sex = temp.getSex();
String id = temp.getID();
String cla = temp.getClasss();
String grade = temp.getGrade();
String location = temp.getLocation();
sql ="insert into student values"+"("+"/""+id+"/""+","+"/""+name+"/""+","+"/""+sex+"/""+","+"/""+age+"/""+","+"/""+grade+"/""+","+"/""+cla+"/""+","+"/""+location+"/""+")"+";";
state.execute(sql);
System.out.println(sql);
}
}
catch(SQLException e){
System.out.println("fuck");
e.printStackTrace();
}catch(Exception e){
System.out.println("you");
e.printStackTrace();
}
}
public void updateToDB(int id,String name,String age,String sex,String grade,String cla,String location){
try{
// UPDATE ±íÃû SET ×Ö¶ÎÃû1=¡¯a',×Ö¶ÎÃû2=¡¯b¡¯ WHERE ×Ö¶ÎÃû3=¡¯c';
Connection conn = DriverManager.getConnection(host,username,password);
Statement state = conn.createStatement();
//ÖŽÐвÙ×÷µÄsqlÓïŸä
String sql = "update student set name="+"/'"+name+"/'"+","+"age="+"/'"+age+"/'"+","+"sex="+"/'"+sex+"/'"+","+"grade="+"/'"+grade+"/'"+","+"class="+"/'"+cla+"/'"+","+"location="+"/'"+location+"/'"+"where id="+"/'"+id+"/'"+";";
System.out.println(sql);
state.execute(sql);
}
catch(SQLException e){
System.out.println("fuck");
e.printStackTrace();
}catch(Exception e){
System.out.println("you");
e.printStackTrace();
}
}
public Student showStudentItem(int flag){
if(flag>0){
// indexItem.remove();
return indexItem.next();
}
else {
//indexItem.remove();
return indexItem.previous();
}
}
/*
public Student showStudentItemReplaceMethod(int index){
// if(loadFromDB.size())
}
*/
public void loadStudentFromDB(){
//Çå¿ÕŒ¯ºÏÀàÖеÄËùÓÐÊýŸÝ£¬ÒÔ±ãÖØÐÂŽÓÊýŸÝ¿âÖжÁÈ¡È«²¿ÊýŸÝ
ResultSet result = null;
loadFromDB.clear();
try{
Connection conn = DriverManager.getConnection(host,username,password);
Statement state = conn.createStatement();
//ÖŽÐвÙ×÷µÄsqlÓïŸä
String sql = "select * from student";
result = state.executeQuery(sql);
//¿éœáÊø
// result.first();
}
catch(SQLException e){
System.out.println("fuck");
e.printStackTrace();
}catch(Exception e){
System.out.println("you");
e.printStackTrace();
}
//String name = null;
try{
while(result.next()){
String name = null;
String age = null;
String id = null;
String sex = null;
String cla = null;
String grade = null;
String location = null;
// String id = null;
name = result.getString("name");
name = new String(name.getBytes("ISO-8859-1"),"GB2312");
//id= result.getString("id");
// id = new String(id.getBytes("ISO-8859-1"),"GB2312");
//age = result.getInt("age");
age = new String(name.getBytes("ISO-8859-1"),"GB2312");
age = new String(age.getBytes("ISO-8859-1"),"GB2312");
id = result.getString("id");
id = new String(id.getBytes("ISO-8859-1"),"GB2312");
sex = result.getString("sex");
sex = new String(sex.getBytes("ISO-8859-1"),"GB2312");
cla = result.getString("class");
cla = new String(cla.getBytes("ISO-8859-1"),"GB2312");
grade = result.getString("grade");
grade = new String(grade.getBytes("ISO-8859-1"),"GB2312");
location = result.getString("location");
location = new String(location.getBytes("ISO-8859-1"),"GB2312");
//System.out.println(result.getString("id")+"/t"+name);
//ŒÓÈëÄÚŽæµÄŒ¯ºÏœá¹¹ÖÐ
Student temp = new Student(id,name,age,sex,location,cla,grade);
// temp.showMeEveryThing();
//System.out.println(temp.toString());
loadFromDB.add(temp);
// System.out.println("size is"+loadFromDB.size());
}
}catch(SQLException e){
System.out.println("love");
e.printStackTrace();
}
catch(Exception e){
System.out.println("hate");
e.printStackTrace();
}
//rs.close();
// conn.close();
}
private static void getConnected(){
String driver = "com.mysql.jdbc.Driver";
// URLÖžÏòÒª·ÃÎʵÄÊýŸÝ¿âÃûschool
String url = "jdbc:mysql://127.0.0.1:3306/school";
// MySQLÅäÖÃʱµÄÓû§Ãû
String user = "root";
// MySQLÅäÖÃʱµÄÃÜÂë
String password = "leon94941";
try {
// ŒÓÔØÇý¶¯³ÌÐò
Class.forName(driver);
// Á¬ÐøÊýŸÝ¿â
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
} catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
public ArrayList<Student> getStudentData(){
return loadFromDB;
}
public void addStudent(Student s){
loadFromDB.add(s);
}
public void flashIterator(){
indexItem.remove();
}
private MrpCore(){
System.out.println("s");
getConnected();
loadStudentFromDB();
indexItem = loadFromDB.listIterator();
System.out.println(loadFromDB.size());
}
public static void main(String[] args){
MrpCore mc = MrpCore.getInstance();
System.out.println(mc.getStudentData().size());
// System.out.println(mc.showStudentItem(-1));
mc.writeToDB();
}
}