第三十五天

配置文件,预处理,分层
例如:

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;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值