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 insert operation
Package com.lykion;
Import java.sql.CallableStatement;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Java.util.Scanner;
public class Dbutil {private static Connection conn = null;
private static PreparedStatement PS = null;
private static ResultSet rs = null;
Private static final CallableStatement cs = null; /** * Insert Method Encapsulation * @param stu incoming parameter */public static void Insert (Stuinfo stu) {conn = Dbconn.conn (); Invoke the Conn () method of the DbConnection class to connect to the database String sql = INSERT into student01 (sno,sname,dname,ssex,cno,mark,type) VALUES (?,?, ?,?,?,?,?)";
Insert SQL statement try {PS = conn.preparestatement (SQL); /** * Call entity Stuinfo class, get the value of each field that needs to be inserted * Note the position of the parameter placeholder * Set the position of the parameter through the Set method * The value of the parameter via the GET method * * ps.setstring (1, Stu
. Getsno ());
Ps.setstring (2, Stu.getsname ());
Ps.setstring (3, Stu.getdname ());
Ps.setstring (4, Stu.getssex ()); Ps.setint (5, stu.geTcno ());
Ps.setdouble (6, Stu.getmark ());
Ps.setstring (7, Stu.gettype ()); Ps.executeupdate ();
Executes the SQL statement SYSTEM.OUT.PRINTLN (insert succeeded (* ̄︶ ̄));
catch (SQLException e) {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", "Wave vegetables", "computer Science and software Engineering "," female ", 8, 99.5," compulsory ");
Dbutil.insert (stu);
}