Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement;ImportJava.util.Scanner; Public classStudent { Public Static voidMain (String args[]) {Try{class.forname ("Com.mysql.jdbc.Driver");//load MySQL JDBC driver//System.out.println ("Success loading Mysql driver!"); } Catch(Exception e) {System.out.print ("Error Loading Mysql driver!"); E.printstacktrace (); } menu (); } Public Static voidmenu () {System.out.println ("****************** Student personal achievement Management System ***********"); System.out.printf ("\ n",NULL); System.out.println ("1, increase results"); System.out.println ("2, revise the results"); System.out.println ("3, delete results"); System.out.println ("4, Query Results"); System.out.println ("5, Show All"); System.out.println ("6, Exit System"); System.out.printf ("\ n",NULL); System.out.println ("************************************************"); System.out.println ("Please enter the function number (1-6):"); Scanner R0=NewScanner (system.in); inti =R0.nextint (); Switch(i) { Case1: Add (); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } Break; Case2: Update (); Break; Case3: Del (); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } Break; Case4: Find (); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } Break; Case5: ShowAll (); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } Break; Case6: Break; default: System.out.println ("Please enter the number 1 to 6"); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } Break; } r0.close (); } Public Static voidAdd () {Scanner SC=NewScanner (system.in); System.out.println ("Please enter new student information, enter the format for the school number, name, and score." such as: 201220201032 Xu 78.5 "); String input=Sc.nextline (); string[] AddItem=input.split (""); if(additem.length==3){ Try{Connection Connect=Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/stumana", "root", "root"); Statement stmt=connect.createstatement (); intrs = stmt.executeupdate ("INSERT into student (id,name,grades) VALUES ('" +additem[0]+ "', '" +additem[1]+ "', '" +additem[2] + "')"); if(rs==1) System.out.println ("Added successfully! "); } Catch(Exception e) {System.out.print ("Get Data error!"); E.printstacktrace (); } } } Public Static voidUpdate () {Scanner SC=NewScanner (system.in); System.out.println ("Please enter student number"); String ID=Sc.nextline (); Try{Connection Connect=Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/stumana", "root", "root"); Statement stmt=connect.createstatement (); ResultSet RS= Stmt.executequery ("SELECT * from student where name= '" +id+ "'"); if(rs!=NULL) {System.out.println ("Please enter new student information, enter the format for the school number, name, and score." such as: 201220201032 Xu 78.5 "); String input=Sc.nextline (); string[] AddItem=input.split (""); if(additem.length==3){ intRs1 = stmt.executeupdate ("Update student set id= '" +additem[0]+ "', Name= '" +additem[1]+ "', grades= '" +additem[2]+ "' where Id= ' "+id+" ' "); if(rs1==1) System.out.println ("Modified successfully!" "); }} free (NULL, stmt, connect); } Catch(Exception e) {System.out.print ("Get Data error!"); E.printstacktrace (); } } Public Static voiddel () {Scanner SC=NewScanner (system.in); System.out.println ("Please enter student number"); String ID=Sc.nextline (); Try{Connection Connect=Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/stumana", "root", "root"); Statement stmt=connect.createstatement (); intrs = stmt.executeupdate ("Delete from student where name= '" +id+ "'"); if(rs==1) System.out.println ("Delete succeeded!" "); Free (NULL, stmt, connect); } Catch(Exception e) {System.out.print ("Get Data error!"); E.printstacktrace (); } } Public Static voidfind () {Scanner SC=NewScanner (system.in); System.out.println ("Please enter student's name"); String name=Sc.nextline (); Try{Connection Connect=Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/stumana", "root", "root"); Statement stmt=connect.createstatement (); ResultSet RS= Stmt.executequery ("SELECT * from student where name= '" +name+ "'"); System.out.print ("id" + ""); System.out.print ("Name" + ""); System.out.println (Results); while(Rs.next ()) {System.out.print (rs.getstring ("id") + ""); System.out.print (Rs.getstring ("Name") + ""); System.out.println (Rs.getfloat ("Grades")); } free (rs,stmt, connect); } Catch(Exception e) {System.out.print ("Get Data error!"); E.printstacktrace (); } } Public Static voidShowAll () {Try{Connection Connect=Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/stumana", "root", "root"); //connection URL is jdbc:mysql//server address/database name, the following 2 parameters are login username and password, respectively//System.out.println ("Success connect Mysql server!");Statement stmt =connect.createstatement (); ResultSet RS= Stmt.executequery ("SELECT * FROM Student"); //User is the name of your tableSystem.out.print ("id" + "" "); System.out.print ("Name" + ""); System.out.println (Results); while(Rs.next ()) {System.out.print (rs.getstring ("id") + ""); System.out.print (Rs.getstring ("Name") + ""); System.out.println (Rs.getfloat ("Grades")); } free (rs,stmt, connect); } Catch(Exception e) {System.out.print ("Get Data error!"); E.printstacktrace (); } } Public Static voidFree (ResultSet resultset,statement st,connection conn) {Try{ if(resultset!=NULL) Resultset.close (); } Catch(Exception e) {e.printstacktrace (); } finally { Try { if(st!=NULL) St.close (); } Catch(Exception e) {e.printstacktrace (); } finally { if(conn!=NULL) Try{conn.close (); } Catch(Exception e) {e.printstacktrace (); } } } }}
JDBC Performance Management System