package connectionutil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionUtil {
private static String URL="jdbc:mysql://localhost:3307/scott"+"?useSSL=false&useUnicode=true"+"&characterEncoding=UTF-8&serverTimezone=UTC";
private static String DRIVER="com.mysql.cj.jdbc.Driver";
private static String USER = "biubiu";
private static String PASSWORD = "123456";
static{
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
return DriverManager.getConnection(URL,USER,PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void close(Connection conn){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package connectionutil;
import java.io.Serializable;
import java.util.Date;
public class Emp implements Serializable {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Double sal;
private Date hiredate;
private Double comm;
private Integer deptno;
public Emp() {
}
public Emp(Integer empno, String ename, String job, Integer mgr, Double sal, Date hiredate, Double comm, Integer deptno) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.sal = sal;
this.hiredate = hiredate;
this.comm = comm;
this.deptno = deptno;
}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
this.mgr = mgr;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public Double getComm() {
return comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", sal=" + sal +
", hiredate=" + hiredate +
", comm=" + comm +
", deptno=" + deptno +
'}';
}
}
package connectionutil.demo;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class Dbuttil {
private static PreparedStatement pst;
private static ResultSet rst;
public static int remove(Connection conn,String sql,Object...params) throws Exception{
pst = conn.prepareStatement(sql);
for (int i =0;i<params.length;i++){
pst.setObject(i+1,params[i]);
}
return pst.executeUpdate();
}
public static int remove(Connection conn, StringBuffer sql, Set<Object> ids)throws Exception{
Iterator<Object> iter = ids.iterator();
while (iter.hasNext()){
sql.append(iter.next()+",");
}
sql.delete(sql.length()-1,sql.length());
sql.append(")");
pst = conn.prepareStatement(sql.toString());
return pst.executeUpdate();
}
public static <T> T selectOne(Connection conn,String sql,Class<T> clz,Object...params) throws Exception{
pst = conn.prepareStatement(sql);
for (int i=0;i<params.length;i++){
pst.setObject(i+1,params[i]);
}
ResultSet rst = pst.executeQuery();
T t = null;
if(rst.next()){
t = clz.newInstance();
Field[] fs = clz.getDeclaredFields();
for(int i =0;i<fs.length;i++){
String fname = fs[i].getName();
Method m = clz.getDeclaredMethod("set"+uppercase(fname),fs[i].getType());
Object fvalue = null;
try {
fvalue = rst.getObject(fname);
}catch (Exception e){
e.printStackTrace();
}
m.invoke(t,fvalue);
}
}
return t;
}
public static <T> List<T> selectList(Connection conn,String sql,Class<T> clz,Object...params) throws Exception{
pst = conn.prepareStatement(sql);
List<T> listObjs = new ArrayList<>();
for(int i = 0;i<params.length;i++){
pst.setObject(i+1,params[i]);
}
ResultSet rst = pst.executeQuery();
T t = null;
while (rst.next()){
t = clz.newInstance();
Field [] fs=clz.getDeclaredFields();
for (int i =0;i<fs.length;i++){
String fname = fs[i].getName();
Method m = clz.getDeclaredMethod("set"+uppercase(fname),fs[i].getType());
Object fvalue = null;
try {
fvalue = rst.getObject(fname);
}catch (Exception e){
e.printStackTrace();
}
m.invoke(t,fvalue)
}
listObjs.add(t);
}
return listObjs;
}
public static int selectCount(Connection conn,String sql,Object...params) throws Exception{
pst = conn.prepareStatement(sql);
for(int i = 0;i<params.length;i++){
pst.setObject(i+1,params[i]);
}
ResultSet rst = pst.executeQuery();
if(rst.next()){
return rst.getInt(1);
}
return 0;
}
public static <T> int save(Connection conn, String sql, T vo, boolean getAutokey) throws Exception {
pst = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
String [] columns = sql.split("\\(")[1].split("\\)")[0].split(",");
Class<?> cls = vo.getClass();
for(int i = 0;i<columns.length;i++){
String column = columns[i];
Method m = cls.getDeclaredMethod("get"+uppercase(column));
Object obj = m.invoke(vo);
pst.setObject(i+1,obj);
}
if(getAutokey){
rst = pst.getGeneratedKeys();
while (rst.next()){
return rst.getInt(1);
}
}else {
return pst.executeUpdate();
}
return 0;
}
public static<T> int edit(Connection conn,String sql,T vo) throws Exception{
pst = conn.prepareStatement(sql);
Class cls = vo.getClass();
String [] columns = sql.split("WHERE")[0].split("SET")[1].trim().split(",");
for (int i = 0;i<columns.length;i++){
String column = columns[i].split("=")[0].trim();
Method m = cls.getDeclaredMethod("get"+uppercase(column));
Object obj = m.invoke(vo);
pst.setObject(i+1,obj);
}
String conditions = sql.split("WHERE")[1].split("=")[0].trim();
Object obj = cls.getDeclaredMethod("get"+uppercase(conditions)).invoke(vo);
pst.setObject(columns.length+1,obj);
return pst.executeUpdate();
}
public static<T> int edit(Connection conn,String sql,Object...params) throws Exception{
pst = conn.prepareStatement(sql);
for (int i = 0;i<params.length;i++){
pst.setObject(i+1,params[i]);
}
return pst.executeUpdate();
}
public static String uppercase(String column){
return column.substring(0,1).toUpperCase()+column.substring(1);
}
}
package connectionutil;
import connectionutil.demo.Dbuttil;
import org.omg.CORBA.OBJECT_NOT_EXIST;
import java.sql.*;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
public class MysqlTest {
public static void main(String[] args) throws Exception {
System.out.println(update("jack","president",10000.0,7904));
}
public static void getEmpByid(Integer id) throws SQLException {
Connection conn = ConnectionUtil.getConnection();
try {
String sql = "SELECT empno,ename,job,hiredate,sal FROM emp WHERE empno ="+id;
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rst = pst.executeQuery();
while (rst.next()){
System.out.println("雇员编号:"+rst.getObject("empno")+",雇员姓名"+rst.getObject("ename")+",入职日期"+
rst.getObject("hiredate")+",薪资"+rst.getObject("sal"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
ConnectionUtil.close(conn);
}
}
public static void getEmpData(String kw,Integer cp,Integer ls) throws SQLException {
if (kw==null){
kw = "";
}
kw="'%"+kw+"%'";
Connection conn = ConnectionUtil.getConnection();
try {
String sql = "SELECT * FROM emp WHERE ename LIKE "+kw+" limit "+(cp-1)*ls+","+ls;
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rst = pst.executeQuery();
while (rst.next()){
System.out.println("雇员编号:"+rst.getObject("empno")+",雇员姓名"+rst.getObject("ename")+",入职日期"+
rst.getObject("hiredate")+",薪资"+rst.getObject("sal"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
ConnectionUtil.close(conn);
}
}
public static boolean deletemp(Set<Integer> ids)throws Exception{
Connection conn = ConnectionUtil.getConnection();
StringBuffer sql = new StringBuffer("DELETE FROM emp where empno in(");
Iterator<Integer> iterator = ids.iterator();
while (iterator.hasNext()){
Integer id = iterator.next();
sql.append(id+",");
}
sql.delete(sql.length()-1,sql.length());
sql.append(")");
PreparedStatement pst=conn.prepareStatement(sql.toString());
int row = pst.executeUpdate();
ConnectionUtil.close(conn);
return row>0;
}
public static boolean update(Emp emp) throws Exception{
Connection conn = ConnectionUtil.getConnection();
String sql = "UPDATE emp SET ename = ?,job = ?,sal = ? WHERE empno = ?";
return Dbuttil.edit(conn,sql,emp)>0;
}
public static boolean update(Object...params) throws Exception{
Connection conn = ConnectionUtil.getConnection();
String sql = "UPDATE emp SET ename = ?,job = ?,sal = ? WHERE empno = ?";
return Dbuttil.edit(conn,sql,params)>0;
}
public static boolean insertEmp(Emp emp) throws Exception{
Connection conn = ConnectionUtil.getConnection();
String sql = "INSERT INTO emp(ename,job,sal) VALUES(?,?,?)";
return Dbuttil.save(conn,sql,emp,false)>0;
}
}
|