您的位置:首页 > 其它

3.使用jdbc完成 crud

2017-10-13 16:57 411 查看
JdbcUtils.java

package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtils {
private static Properties config = new Properties();
static{

try {

config.load(JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"));
Class.forName(config.getProperty("driver"));
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}

}

public static Connection getConnection()throws Exception{

return DriverManager.getConnection(config.getProperty("url"),config.getProperty("username"),config.getProperty("password"));

}
public static void release(Connection conn,Statement st,ResultSet rs) {
if(rs != null){
try {
rs.close();
} catch (Exception e) {

e.printStackTrace();
}
rs = null;
}
if(st != null){
try {
st.close();
} catch (Exception e) {

e.printStackTrace();
}
st = null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
}
}
}


db.properties

url=jdbc:mysql://localhost:3306/mydb2
driver = com.mysql.jdbc.Driver
username = root
password = root

Users.java
package domain;

import java.sql.Date;

public class Users {
private int id;
String username;
String password;
Date birthday;
Date entry_date;
String job;
String salary;
String ersume;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getEntry_date() {
return entry_date;
}
public void setEntry_date(Date entry_date) {
this.entry_date = entry_date;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public String getErsume() {
return ersume;
}
public void setErsume(String ersume) {
this.ersume = ersume;
}

}


Sql2.java

package mysql2;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import domain.Users;
import utils.JdbcUtils;

public class Sql2 {

public static void main(String[] args) {
// TODO Auto-generated method stub

}

public void insert() throws Exception{
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
conn = JdbcUtils.getConnection();
st = conn.createStatement();
String sql = "insert into employee values(5,'貂蝉','352-7-5','2110-1-1','奉先之爱','6000.0','噬神之书')";
int num = st.executeUpdate(sql);
if(num>0){
System.out.println("插入成功!");
}
}finally{
JdbcUtils.release(conn, st, rs);
}
}

public void update() throws Exception{
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
conn = JdbcUtils.getConnection();
st = conn.createStatement();
String sql = "update employee set ersume='纯阳剑' where id='3'";
int num = st.executeUpdate(sql);
if(num>0){
System.out.println("更新成功!");
}
}finally{
JdbcUtils.release(conn, st, rs);
}
}
@Test
public void getAll() throws Exception{
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
conn = JdbcUtils.getConnection();
st = conn.createStatement();
String sql = "select * from employee";
rs= st.executeQuery(sql);
List list = new ArrayList();
while(rs.next()){
Users user = new Users();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
list.add(user);
}
System.out.println(list);
}finally{
JdbcUtils.release(conn, st, rs);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: