源代码如下:
DBUtil.java:
packagedao;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.sql.PreparedStatement;public classDBUtil {public static String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT";public static String db_user = "root";public static String db_pass = "root";public staticConnection getConn () {
Connection conn= null;try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection(db_url, db_user, db_pass);
}catch(Exception e) {
e.printStackTrace();
}returnconn;
}public static voidclose (Statement state, Connection conn) {if (state != null) {try{
state.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if (conn != null) {try{
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}public static voidclose (ResultSet rs, Statement state, Connection conn) {if (rs != null) {try{
rs.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if (state != null) {try{
state.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if (conn != null) {try{
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}}
Add.java:
packageadd;importjava.sql.Connection;importjava.sql.Statement;importdao.DBUtil;public classAdd {public static booleanadd(String table, AddService user ) {
String sql= "insert into "+table+"(username,password)values('" + user.getUsername() + "','" + user.getPassword() + "')";
Connection conn=DBUtil.getConn();
Statement state= null;boolean f = false;int a = 0;try{
state=conn.createStatement();
a=state.executeUpdate(sql);
}catch(Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(state, conn);
}if (a > 0) {
f= true;
}returnf;
}
}
AddService.java:
packageadd;public classAddService {
String username;
String password;publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}public static voidmain(String args[]){
AddService user=newAddService();
user.setUsername("123");
user.setPassword("456");
Add test=newAdd();
test.add("user1",user);
}
}
Delete.java:
packagedelete;importjava.sql.Connection;importjava.sql.Statement;importjava.sql.SQLException;importdao.DBUtil;public classDelete {public booleandelete(String table,String username)
{boolean c=false;
Connection conn=DBUtil.getConn();
Statement state=null;
String sql="delete from "+table+" where username="+username;try{
state=conn.createStatement();int num =state.executeUpdate(sql);if(num!=0)
{
c= true;
}
state.close();
conn.close();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnc;
}
}
DeleteService.java:
packagedelete;public classDeleteService {
String username;public voidsetUsername(String username) {this.username =username;
}publicString getUsername() {returnusername;
}public static voidmain(String args[]){
DeleteService user=newDeleteService();
user.setUsername("123");
String username="'"+user.getUsername()+"'";
Delete test=newDelete();
test.delete("user1",username);
}
}
Change.java:
packagechange;importjava.sql.Connection;importjava.sql.SQLException;importjava.sql.Statement;importdao.DBUtil;public classChange {public booleanchange(String table,String lie,String lie0,String gai,String biao)
{
Connection conn=DBUtil.getConn();
Statement state=null;try{
state=conn.createStatement();
String sql="update "+table+" set "+lie+"='"+gai+"' where "+lie0+"='"+biao+"'";
System.out.println(sql);
state.executeUpdate(sql);
state.close();
conn.close();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}return true;
}
}
ChangeService.java:
packagechange;public classChangeService {
String lie;
String lie0;
String gai;
String biao;publicString getBiao() {returnbiao;
}publicString getGai() {returngai;
}publicString getLie() {returnlie;
}publicString getLie0() {returnlie0;
}public voidsetBiao(String biao) {this.biao =biao;
}public voidsetGai(String gai) {this.gai =gai;
}public voidsetLie(String lie) {this.lie =lie;
}public voidsetLie0(String lie0) {this.lie0 =lie0;
}public static voidmain(String args[]){
ChangeService user=newChangeService();
user.setBiao("2");
user.setGai("xhj");
user.setLie0("username");
user.setLie("password");
Change test=newChange();
test.change("user1",user.getLie(),user.getLie0(),user.getGai(),user.getBiao());
}
}
Select.java:
packageselect;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importdao.DBUtil;public classSelect {public booleanselect(String table)
{booleanc;
Connection conn=DBUtil.getConn();
Statement state=null;try{
state=conn.createStatement();
String sql="select * from "+table;
ResultSet rs=state.executeQuery(sql);while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2));
}
rs.close();
state.close();
conn.close();
}catch(Exception e)
{
}return true;
}
}
SelectService.java:
packageselect;public classSelectService {
String table;publicString getTable() {returntable;
}public voidsetTable(String table) {this.table =table;
}public static voidmain(String[] args) {
SelectService user=newSelectService();
user.setTable("user1");
Select test=newSelect();
test.select(user.getTable());
}
}
数据库表名:user1