Create 4 classes: Stuinfo (entity Class), Dbconn (connect, close database), Dbutil (Database Operations), Runmain (test Class)
1.StuInfo Entity classes:
Package com.lykion;
public class Stuinfo {private String sno;
Private String sname;
Private String dname;
Private String Ssex;
private int cno;
Private double mark;
Private String type; Public Stuinfo () {} public Stuinfo (String sno, String sname, String dname, string ssex, int cno, double mark, Stri
ng type) {super ();
This.sno = Sno;
This.sname = sname;
This.dname = dname;
This.ssex = Ssex;
This.cno = CNO;
This.mark = Mark;
This.type = type;
Public String Getsno () {return sno;
} public void Setsno (String sno) {This.sno = sno;
Public String Getsname () {return sname;
} public void Setsname (String sname) {this.sname = sname;
Public String Getdname () {return dname;
} public void Setdname (String dname) {this.dname = dname;
Public String Getssex () {return ssex;
} public void Setssex (String ssex) {this.ssex = Ssex;
public int Getcno () {return CNO; } public void Setcno (int cno) {This.cno = CNO;
Public double Getmark () {return mark;
public void Setmark (double mark) {This.mark = Mark;
Public String GetType () {return type;
public void SetType (String type) {this.type = type;
}
}
2.DBConn class, operations on the database: Open the database (connect to the database), close the database after the operation completes, release the resources
Package com.lykion;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException; public class Dbconn {private static final String URL = "Jdbc:mysql://localhost:3306/test"; Database address private static final String username = "root"; Database user name private static final String password = "123456"; Database Password private static final String Driver = "Com.mysql.jdbc.Driver";
MySQL driver private static final Connection conn = null;
/** * Connection Database * @return/public static Connection conn () {Connection conn = null; try {class.forname (driver); Load Database Drive try {conn = drivermanager.getconnection (URL, username, password);
Connection database} catch (SQLException e) {e.printstacktrace ();
} catch (ClassNotFoundException e) {e.printstacktrace ();
Return conn; /** * Closes the database link * @return/public static void close () {if (conn!= null) {try {conn.close ();
Close Database link} catch (SQLException e) {e.printstacktrace (); }
}
}
}
3.DBUtil class: Mainly used in data operations, the main example is to implement the update operation
Package com.lykion;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
public class Dbutil {private static Connection conn = null;
private static PreparedStatement PS = null;
private static ResultSet rs = null; public static void Update (Stuinfo stu) {conn = Dbconn.conn (); Invoke the Conn () method of the DbConnection class to connect to the database String sql = UPDATE student01 SET sname=?, Dname=, ssex=, cno=? WHERE sno=? ";
SQL statement try {PS = conn.preparestatement (SQL); /** * Sno Modified (updated) Student information according to school number * 1. The position of the incoming parameter will change. * 2.sno position is not in the first position, but at the end * 3. Set the position of each field exactly * * * ps.setstring (1
, Stu.getsname ());
Ps.setstring (2, Stu.getdname ());
Ps.setstring (3, Stu.getssex ());
Ps.setint (4, Stu.getcno ());
Ps.setdouble (5, Stu.getmark ());
Ps.setstring (6, Stu.gettype ());
Ps.setstring (7, Stu.getsno ());
Ps.executeupdate ();
SYSTEM.OUT.PRINTLN ("Modification succeeds (* ̄︶ ̄)"); catch (SQLException e) {SyStem.out.println ("Operation failed O (╥﹏╥)");
E.printstacktrace ();
}finally {dbconn.close ();
}
}
}
4.RunMain class: Test class, test whether the method in Dbutil is implemented
Package com.lykion;
public class Runmain {public
static void Main (string[] args) {
stuinfo stu = new Stuinfo ("9006", "glutinous rice dumpling", "Computer Science and soft Piece works "," female ", 7, 89," elective ");
Dbutil.update (stu); }