package jdbk;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Conn {
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class con_test {
private static final String URL = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false";
private static final String USER = "root";
private static final String PASS = "123456";
static Connection conn = null;
static PreparedStatement ps = null;
public static void init() {
if(conn == null) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASS);
}catch(Exception e) {
e.printStackTrace();
System.out.println("build conncetion failed");
}
System.out.println("build success");
}
else {
System.out.println("success");
}
}
public static void query(String sql) {
con_test.init();
try {
ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getInt("id")+rs.getString("stu_name"));
}
}catch(Exception e) {
e.printStackTrace();
System.out.println("sql failed");
}
}
public static void update(String sql) {
con_test.init();
try {
ps = conn.prepareStatement(sql);
int rs = ps.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
System.out.println("sql failed");
}
}
public static boolean add(String name,int sex) {
con_test.init();
String sql = "insert into stu_info(stu_name,sex) values(?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1,name);
ps.setInt(2, sex);
return ps.execute();
}catch(Exception e) {
e.printStackTrace();
return false;
}
}
public static int dealBatch(String[] sqls) {
con_test.init();
try {
conn.setAutoCommit(false);
Statement st = conn.createStatement();
for(int i = 0; i<sqls.length;i++) {
st.addBatch(sqls[i]);
}
st.executeBatch();
conn.commit();
return 1;
}catch(Exception e) {
e.printStackTrace();
return 0;
}
}
public static Integer addReturnPrimaryKey(String sql) {
con_test.init();
try {
ps = conn.prepareStatement(sql, ps.RETURN_GENERATED_KEYS);
int re = ps.executeUpdate();
if(re == 0) {
return 0;
}
ResultSet rs2 = ps.getGeneratedKeys();
Integer id = null;
if(rs2.next()) {
id = rs2.getInt(1);
}
return id;
}catch(SQLException e) {
e.printStackTrace();
System.out.println("失败");
return null;
}
}
public static void close() {
if(ps != null) ps = null;
if(conn != null) conn = null;
}
public static void main(String[] args) {
String[] sqls = new String[3];
sqls[0] = "insert into stu_info(id,stu_name) values(29,'侠客')";
sqls[1] = "update stu_info set stu_name = '管老八' where id = 5";
sqls[2] = "delete from stu_info where id = 57";
String sql = "insert into stu_info(id,stu_name) values(77,'李大明')";
Integer key = addReturnPrimaryKey(sql);
System.out.println(key);
}
}
package jdbc;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Student;
public class Conn2 {
private static final String URL = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false";
private static final String USER = "root";
private static final String PASS = "123456";
static Connection conn = null;
static PreparedStatement ps = null;
static ResultSet re = null;
public static void init() {
if(conn == null) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASS);
}catch(Exception e) {
e.printStackTrace();
System.out.println("build conncetion failed");
}
System.out.println("build success");
}
else {
System.out.println("success");
}
}
public static String getParam(String column) {
String[] arr = column.split("_");
StringBuffer sf = new StringBuffer(arr[0]);
for(int i = 1;i<arr.length;i++) {
sf.append(arr[i].substring(0,1).toUpperCase()+arr[i].substring(1));
}
return sf.toString();
}
public static <T> List<T> queryAll(String sql,Class<T> cls,Object ... params){
Conn2.init();
try {
ps = conn.prepareStatement(sql);
if(params != null) {
for(int i = 0;i<params.length;i++) {
ps.setObject(i+1, params[i]);
}
}
re = ps.executeQuery();
ResultSetMetaData rmd = re.getMetaData();
List<T> list = new ArrayList<T>();
while(re.next()) {
T t = cls.newInstance();
for(int i = 0;i<rmd.getColumnCount();i++) {
try {
String column = rmd.getColumnLabel(i+1);
Object value = re.getObject(column);
Field field = cls.getDeclaredField(column);
field.setAccessible(true);
field.set(t, value);
}catch(Exception e) {
}
}
list.add(t);
}
return list;
}catch(Exception e) {
e.printStackTrace();
return null;
}finally {
if(conn != null) {
conn = null;
}
if(ps != null) {
ps = null;
}
if(re != null) {
re = null;
}
}
}
public static void main(String[] args) {
String sql = "select * from stu_info where id = ? and stu_name = ?";
List<Student> list = queryAll(sql, Student.class,11,"谢大脚");
for(Student stu : list) {
stu.print();
}
}
}