昨天发表的CSDN代码还略显不足,今日通过学习小龙老师的jdbc对其进行修改和完善。
问题7:在DAO方法中拼接SQL语句,很恶心使用PreparedStatement解决.
总体架构图片:
具体实现代码:
0、db.properties
driver=jdbc:mysql://localhost:3306/productdemo
username=root
password=123456
conect=com.mysql.jdbc.Driver
1、jdbcutil类
package jdbc.mis.util;
import java.io.IOException;
import java.io.InputStream;
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 jdbcutil {
private static Properties p=new Properties();
static {
ClassLoader clo=Thread.currentThread().getContextClassLoader();
InputStream in=clo.getResourceAsStream("db.properties");
try {
p.load(in);
Class.forName(p.getProperty("conect"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getconn()
{
try {
return DriverManager.getConnection(p.getProperty("driver"), p.getProperty("username"), p.getProperty("password"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
throw new RuntimeException("数据库连接异常");
}
public static void close(Connection conn,Statement st,ResultSet re)
{
try {
if(re!=null)
re.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
finally {
try {
if(st!=null)
st.close();
} catch (Exception e3) {
// TODO: handle exception
e3.printStackTrace();
}finally {
try {
if(conn!=null)
conn.close();
} catch (Exception e4) {
// TODO: handle exception
e4.printStackTrace();
}
}
}
}
}
2、IStudentDAOimpl接口
package jdbc.mis.dao.impl;
import java.util.List;
import jdbc.mis.domain.Student;
public interface StudentDAOimpl {
/**
* 保存用户对象
* @param stu
*/
void add(Student stu);
/**
* 修改用户对象
* @param stu
*/
void update(Student stu);
/**
* 删除用户对象
* @param id
*/
void delete(long id);
/**
* 查询用户对象,并且返回一个对象
* @param stu
* @return
*/
/**
* 查询用户对象,并且返回多个对象
* @return
*/
List<Student> list();
Student getone(long id);
}
2、IStudentDAO实现类(用/**/括起来的方法即昨日的旧方法)
package jdbc.mis.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.sql.PreparedStatement;
import jdbc.mis.dao.impl.StudentDAOimpl;
import jdbc.mis.domain.Student;
import jdbc.mis.util.jdbcutil;
public class IStudentDAO implements StudentDAOimpl {@Override
public void add(Student stu) {
// TODO Auto-generated method stub
//String sql1="INSERT INTO t_student (name,age)VALUES(stu.getName(),stu.getAge())";
String sql="INSERT INTO t_student (name,age)VALUES(?,?)";
Connection conn=null;
PreparedStatement st=null;
/*
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
try {
//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/productdemo", "root", "123456");
conn=jdbcutil.getconn(); //新方法
//st=conn.createStatement();
st=conn.prepareStatement(sql);
st.setString(1,"星驰");
st.setInt(2, 56);
int row=st.executeUpdate();
if(row!=0)
System.out.println("插入成功");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
/*
try {
if(st!=null)
st.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
finally {
try {
if(conn!=null)
conn.close();
} catch (Exception e3) {
// TODO: handle exception
e3.printStackTrace();
}
}*/
jdbcutil.close(conn, st, null); //新资源关闭方法
}
}
@Override
public void update(Student stu) {
// TODO Auto-generated method stub
//String sql1="UPDATE t_student SET name=stu.getname,age=stu.getage where id='+stu.getid+'";
String sql="UPDATE t_student SET name=?,age=? where id=?";
Connection conn=null;
PreparedStatement st=null;
/*
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
try {
//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/productdemo", "root", "123456");
conn=jdbcutil.getconn(); //新方法
//st=conn.createStatement();
st=conn.prepareStatement(sql);
st.setString(1, "威龙");
st.setInt(2, 20);
st.setLong(3,7);
//System.out.println(st.executeQuery());
int row=st.executeUpdate();
if(row!=0)
System.out.println("修改成功");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {/*
try {
if(st!=null)
st.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
finally {
try {
if(conn!=null)
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}*/
jdbcutil.close(conn, st, null); //新资源关闭方法
}
}
@Override
public void delete(long id) {
// TODO Auto-generated method stub
String sql="DELETE FROM t_student where name=? or name=?";
Connection conn=null;
PreparedStatement st=null;
/*
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
try {
//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/productdemo", "root", "123456");
conn=jdbcutil.getconn(); //新方法
//st=conn.createStatement();
st=conn.prepareStatement(sql);
st.setString(1,"长城");
st.setString(2,"飞鲨");
int row=st.executeUpdate();
if(row!=0)
System.out.println("删除成功");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {/*
try {
if(st!=null)
st.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
finally {
try {
if(conn!=null)
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}*/
jdbcutil.close(conn, st, null); //新资源关闭方法
}
}
@Override
public Student getone(long id) {
// TODO Auto-generated method stub
String sql="SELECT *FROM t_student where id=?";
Connection conn=null;
PreparedStatement st=null;
ResultSet re=null;
/*
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}*/
try {
//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/productdemo", "root", "123456");
conn=jdbcutil.getconn(); //新方法
st=conn.prepareStatement(sql);
st.setLong(1, id);
re=st.executeQuery();
//st=conn.createStatement();
//re=st.executeQuery(sql);
if(re.next())
{
Student stu=new Student();
stu.setAge(re.getInt("age"));
stu.setId(re.getLong("id"));
stu.setName(re.getString("name"));
return stu;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {/*
try {
if(re!=null)
re.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
finally {
try {
if(st!=null)
st.close();
} catch (Exception e3) {
// TODO: handle exception
e3.printStackTrace();
}finally {
try {
if(conn!=null)
conn.close();
} catch (Exception e4) {
// TODO: handle exception
e4.printStackTrace();
}
}
}*/
jdbcutil.close(conn, st, re); //新资源关闭方法
}
return null;
}
@Override
public List<Student> list() {
// TODO Auto-generated method stub
List<Student>list=new ArrayList<>();
String sql="SELECT *FROM t_student";
Connection conn=null;
PreparedStatement st=null;
ResultSet re=null;
/*
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}*/
try {
//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/productdemo", "root", "123456");
conn=jdbcutil.getconn(); //新方法
//st=conn.createStatement();
st=conn.prepareStatement(sql);
re=st.executeQuery();
while(re.next())
{
Student stu=new Student();
stu.setAge(re.getInt("age"));
stu.setId(re.getLong("id"));
stu.setName(re.getString("name"));
list.add(stu);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {/*
try {
if(re!=null)
re.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
finally {
try {
if(st!=null)
st.close();
} catch (Exception e3) {
// TODO: handle exception
e3.printStackTrace();
}finally {
try {
//if(conn!=null)
//conn.close();
} catch (Exception e4) {
// TODO: handle exception
e4.printStackTrace();
}
}
}
}*/
jdbcutil.close(conn, st, re); //新资源关闭方法
}
return list;
}
}
4、IStudentTest测试类
package jdbc.mis.IStudentTest;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import jdbc.mis.dao.IStudentDAO;
import jdbc.mis.domain.*;
public class StudentTest {
@Test
public void addtest() throws Exception {
IStudentDAO stu=new IStudentDAO();
Student s=new Student();
s.setAge(20);
s.setName("周周");
stu.add(s);
}
@Test
public void updatetest() throws Exception {
IStudentDAO stu=new IStudentDAO();
Student s=new Student();
s.setId(5);
s.setAge(21);
s.setName("lulu");
stu.update(s);
}
@Test
public void deletetest() throws Exception {
IStudentDAO stu=new IStudentDAO();
stu.delete(12);
}
@Test
public void gettest() throws Exception {
IStudentDAO stu=new IStudentDAO();
System.out.println(stu.getone(11));
}
@Test
public void listtest() throws Exception {
IStudentDAO stu=new IStudentDAO();
for (Student ele : stu.list()) {
System.out.println(ele);
}
}
}