Connect the remote database with Java, write a small Java GUI program, write a small table student (number,name,sex), can write data, delete data, query data
Main code:
1 Public classTestsqlonframe {2 Public Static voidMain (string[] args) {3Frame frameone=Newframe ();4 Frameone.main ();5 }6 }7 8 classFrameextendsframe{9 //New ComponentTenFrame frameone=NewFrame ("My Java GUI"); One ATextField textone=NewTextField (5); -TextField texttwo=NewTextField (5); -TextField textthree=NewTextField (5); the -Label labelone=NewLabel ("number"); -Label labeltwo=NewLabel ("name"); -Label labelthree=NewLabel ("Sex"); + -Button buttonone=NewButton ("Set"); +Button buttontwo=NewButton ("Search"); AButton buttonthree=Newbutton ("delete"); atLabel labelforinformation=NewLabel (); -Font fconsole=NewFont ("Console", 0, 20); - - Public voidMain () { - //Component Properties -Frameone.setlayout (NewGridLayout (4,3)); inFrameone.setsize (600,300); -Frameone.setlocation (300,300); to Frameone.setfont (fconsole); + //Component Listener -Frameone.addwindowlistener (Newhandlewidows ()); theButtonone.addactionlistener (NewHandlebutton ()); *Buttontwo.addactionlistener (NewHandlebutton ()); $Buttonthree.addactionlistener (NewHandlebutton ());Panax Notoginseng //adding components to a frame - Frameone.add (labelone); the Frameone.add (labeltwo); + Frameone.add (labelthree); A Frameone.add (textone); the Frameone.add (texttwo); + Frameone.add (textthree); - Frameone.add (buttonone); $ Frameone.add (buttontwo); $ Frameone.add (buttonthree); - Frameone.add (labelforinformation); -Frameone.add (NewLabel ()); theFrameone.add (NewLabel ()); -Frameone.setvisible (true);Wuyi the } - //Inner class listener action Wu classHandlebuttonImplementsactionlistener{ - Public voidactionperformed (ActionEvent e) { About Try { $ //new driver, registration driver -Com.mysql.jdbc.Driver driverone=Newcom.mysql.jdbc.Driver (); - Drivermanager.registerdriver (driverone); - //connect the three parameters to establish a connection AString url= "jdbc:mysql://your database server IP address: 3306/Your database name"; +String name= "Your user name"; theString password= "Your password"; -Connection conn=drivermanager.getconnection (Url,name,password); $ //To establish a SQL Messenger: theStatement st=(Statement) conn.createstatement (); theString sql= ""; the //resultset used to receive queries the ResultSet rs; - //button Actions, respectively, insert select Delete in if(E.getsource () = =buttonone) { the //Add to Database theString numberwillbeadded=Textone.gettext (); AboutSql= "INSERT into student values (" +Textone.gettext () the+ ", \ '" +texttwo.gettext () + "\" the+ ", \ '" +Textthree.gettext () the+ "\");; + System.out.println (SQL); - st.executeupdate (SQL); theLabelforinformation.settext (numberwillbeadded+ "added");Bayi } the if(E.getsource () = =buttontwo) { the //Search -Sql= "SELECT * from student where number" -+ "=" +textone.gettext () + ";"; thers=st.executequery (SQL); the System.out.println (SQL); the if(Rs.next () = =true) { theLabelforinformation.settext ("Find it!"); -}Else{ theLabelforinformation.settext ("fail to find it"); the } theTexttwo.settext ("");94Textthree.settext (""); theTexttwo.settext (rs.getstring ("name")); theTextthree.settext (rs.getstring ("Sex"))); the }98 if(E.getsource () = =buttonthree) { About //Delete -String numberwillbedeteled=Textone.gettext ();101Sql= "Delete from student where number="102+textone.gettext () + ";";103 st.executeupdate (SQL);104Labelforinformation.settext (numberwillbedeteled+ "deleted"); the }106}Catch(SQLException E1) {107 //Todo:handle Exception108 }109 } the }111 classHandlewidowsextendswindowadapter{ the Public voidwindowclosing (windowevent e) {113System.exit (0); the } the } the 117}
Of course, remember to do a good job of MySQL driver and add the build path
Write a Java connection to the remote MySQL database once