JDBC对表的增添改查
    [size=x-large][/size][color=darkblue][/color]
第一种写法:
public class Stu {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Stu(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Stu(){}
}
import java.sql.SQLException;
import java.util.ArrayList;
public interface StuDeal {
	public  void insert(Stu stu) throws SQLException;
	public  void delete(int id)throws SQLException;
	
	public  void query(Stu stu)throws SQLException;
	
	public  ArrayList findCondition(Stu stu)throws SQLException;
}
/*
 * 关闭各种资源的工具类
 * 
 * **/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
public class Util {
	public static Connection getConnection(){
		Connection conn=null;
		try{
		Class.forName("com.mysql.jdbc.Driver");
		String url="jdbc:mysql://localhost:3306/test";
		conn=DriverManager.getConnection(url,"root","1");}catch(Exception e){e.printStackTrace();}
		return conn;
	}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class StuDao implements StuDeal{
	public  void insert(Stu stu) throws SQLException {
		Connection conn = Util.getConnection();
		PreparedStatement ps = conn
				.prepareStatement("insert into stu values(?,?)");
		ps.setInt(1, stu.getId());
		ps.setString(2, stu.getName());
		ps.executeUpdate();
	}
	public  void delete(int id) throws SQLException {
		Connection conn = Util.getConnection();
		PreparedStatement ps = conn
				.prepareStatement("delete from stu where id=?");
		ps.setInt(1, id);
		ps.executeUpdate();
	}
	public  void query(Stu stu) throws SQLException {
		Connection conn = Util.getConnection();
		PreparedStatement ps = conn
				.prepareStatement("select * from stu where id=?");
		ps.setInt(1, stu.getId());
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			System.out.println("id号为:" + rs.getInt(1) + "  " + "名字为:"
					+ rs.getString(2));
		}
		sta.close();
		rs.close();
		conn.close();
	}
	public  ArrayList findCondition(Stu stu)throws SQLException{
		ArrayList <Stu>s=new ArrayList();
		String sql="select id,name from stu where 1=1 and ";
		if(stu.getId()!=0){
			sql=sql+" id="+stu.getId()+" and ";
		}
		if(stu.getName()!=null){
			sql=sql+" name ='"+stu.getName()+"' and";
		}
		if(sql.endsWith(" and")){
			sql=sql.substring(0, sql.length()-3);
		}
		Connection conn = Util.getConnection();
		Statement sta=conn.createStatement();
		ResultSet rs=sta.executeQuery(sql);
		while(rs.next()){
			Stu st=new Stu();
			st.setId(rs.getInt(1));
			st.setName(rs.getString(2));
			s.add(st);
		}
		sta.close();
		rs.close();
		conn.close();
		return s;
		
	}
}
import java.sql.SQLException;
import java.util.ArrayList;
public class StuTest {
	/**
	 * @param args
	 * @throws SQLException 
	 */
	public static void main(String[] args) th