源代码如下:
DBUtil.java:
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
public class DBUtil {
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 static Connection 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();
}
return conn;
}
public static void close (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 void close (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:
package add;
import java.sql.Connection;
import java.sql.Statement;
import dao.DBUtil;
public class Add {
public static boolean add(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;
}
return f;
}
}
AddService.java:
package add;
public class AddService {
String username;
String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public static void main(String args[]){
AddService user=new AddService();
user.setUsername("123");
user.setPassword("456");
Add test=new Add();
test.add("user1",user);
}
}
Delete.java:
package delete;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import dao.DBUtil;
public class Delete {
public boolean delete(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();
}
return c;
}
}
DeleteService.java:
package delete;
public class DeleteService {
String username;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public static void main(String args[]){
DeleteService user=new DeleteService();
user.setUsername("123");
String username="'"+user.getUsername()+"'";
Delete test=new Delete();
test.delete("user1",username);
}
}
Change.java:
package change;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import dao.DBUtil;
public class Change {
public boolean change(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:
package change;
public class ChangeService {
String lie;
String lie0;
String gai;
String biao;
public String getBiao() {
return biao;
}
public String getGai() {
return gai;
}
public String getLie() {
return lie;
}
public String getLie0() {
return lie0;
}
public void setBiao(String biao) {
this.biao = biao;
}
public void setGai(String gai) {
this.gai = gai;
}
public void setLie(String lie) {
this.lie = lie;
}
public void setLie0(String lie0) {
this.lie0 = lie0;
}
public static void main(String args[]){
ChangeService user=new ChangeService();
user.setBiao("2");
user.setGai("xhj");
user.setLie0("username");
user.setLie("password");
Change test=new Change();
test.change("user1",user.getLie(),user.getLie0(),user.getGai(),user.getBiao());
}
}
Select.java:
package select;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import dao.DBUtil;
public class Select {
public boolean select(String table)
{
boolean c;
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:
package select;
public class SelectService {
String table;
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
public static void main(String[] args) {
SelectService user=new SelectService();
user.setTable("user1");
Select test=new Select();
test.select(user.getTable());
}
}
数据库表名:user1