配置文件,预处理,分层
例如:
package com.bu.dao;
import com.bu.util.JDBCTemplete;
public class PetDao {
public static void main(String[] args) {
JDBCTemplete jt = new JDBCTemplete();
String sql = "update pet set pname = '汪汪' where pid = 1 ";
jt.updateInsertAndDelete(sql);
}
}
package com.bu.po;
public class Pet {
private Integer pid;
private String pname;
private String psex;
private String type;
private Integer age;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPsex() {
return psex;
}
public void setPsex(String psex) {
this.psex = psex;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Pet [pid=" + pid + ", pname=" + pname + ", psex=" + psex
+ ", type=" + type + ", age=" + age + "]";
}
}
package com.bu.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ConfigManager {
public static String DRIVER;
public static String URL;
public static String USERNAME;
public static String PASSWORD;
static {
Properties p = new Properties();
try {
p.load(new FileInputStream("D:\\77777\\JDBC\\src\\com\\hrbu\\util\\config.properties"));
DRIVER = p.getProperty("driver");
URL = p.getProperty("url");
USERNAME = p.getProperty("username");
PASSWORD = p.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.hrbu.util;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import com.hrbu.po.Pet;
public class JDBCTemplete {
public int updateInsertAndDelete(String sql, Object...objs){
int i = 0;
Connection conn = null;
PreparedStatement pstate = null;
//创建驱动
try {
Class.forName(ConfigManager.DRIVER);
//创建连接
conn = DriverManager.getConnection(ConfigManager.URL, ConfigManager.USERNAME,ConfigManager.PASSWORD);
//创建状态参数
//预处理
pstate = conn.prepareStatement(sql);
for (int m=0;m<objs.length;m++){
pstate.setObject(m+1,objs[m]);
}
//执行sql,并获取返回值
i = pstate.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
//输出
System.out.print(i);
//关闭
try {
pstate.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return i;
}
private static List selectPets(String sql){
Connection conn = null;
Statement state = null;
List list = new ArrayList();
//创建驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//创建连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lianxi","root","root");
//创建状态参数
state = conn.createStatement();
//执行sql,并获取返回值
ResultSet rs = state.executeQuery(sql);
while(rs.next()){
Pet p = new Pet();
p.setPid(rs.getInt(1));
p.setPname(rs.getString(2));
p.setPsex(rs.getString(3));
p.setType(rs.getString(4));
p.setAge(rs.getInt(5));
list.add(p);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}