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) throws SQLException {
// TODO Auto-generated method stub
    /* Stu s=new Stu();
     s.setName("d");
     StuDeal sdao=new StuDao();
     ArrayList <Stu>list=sdao.findCondition(s);
    for(int i=0;i<list.size();i++){
    System.out.println("学生id为:"+list.get(i).getId()+"学生姓名为:"+list.get(i).getName());
    }*/
StuDeal sdao=new StuDao();
Stu s=new Stu(9,"c");
sdao.insert(s);


}

}















第二种写法



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();
Statement sta=conn.createStatement();
String sql="insert into stu values(" +stu.getId()+ "," + "'" + stu.getName()+ "'" + ") ";
sta.executeUpdate(sql);
sta.close();
conn.close();
}

public  void delete(int id) throws SQLException {
Connection conn = Util.getConnection();
Statement sta=conn.createStatement();
sta.executeUpdate("delete from stu where id="+id);
sta.close();
conn.close();
}

public  void query(Stu stu) throws SQLException {
Connection conn = Util.getConnection();
Statement sta=conn.createStatement();
ResultSet rs=sta.executeQuery("select * from stu where id="+stu.getId());
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) throws SQLException {
// TODO Auto-generated method stub
    /* Stu s=new Stu();
     s.setName("d");
     StuDeal sdao=new StuDao();
     ArrayList <Stu>list=sdao.findCondition(s);
    for(int i=0;i<list.size();i++){
    System.out.println("学生id为:"+list.get(i).getId()+"学生姓名为:"+list.get(i).getName());
    }*/
StuDeal sdao=new StuDao();
Stu s=new Stu(9,"c");
sdao.insert(s);


}

}






第三种写法



import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class StuFactory {
private static Connection conn=null;
private static Statement sta=null;
private static ResultSet rs=null;
public static Statement getStatement() throws SQLException{

    conn=Util.getConnection();

sta=conn.createStatement();
return sta;
}
public static void closeStu(Statement sta){
if(sta!=null){try{
sta.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn!=null){try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}

}
public static void closeStu(Statement sta,ResultSet rs){
if(sta!=null){try{
sta.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(rs!=null){try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn!=null){try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}

}
}






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




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.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 {
Statement sta=StuFactory.getStatement();
String sql="insert into stu values(" +stu.getId()+ "," + "'" + stu.getName()+ "'" + ") ";
sta.executeUpdate(sql);
StuFactory.closeStu(sta);
}

public  void delete(int id) throws SQLException {
Statement sta=StuFactory.getStatement();
sta.executeUpdate("delete from stu where id="+id);
StuFactory.closeStu(sta);
}

public  void query(Stu stu) throws SQLException {
Statement sta=StuFactory.getStatement();
ResultSet rs=sta.executeQuery("select * from stu where id="+stu.getId());
while (rs.next()) {
System.out.println("id号为:" + rs.getInt(1) + "  " + "名字为:"
+ rs.getString(2));
}
StuFactory.closeStu(sta,rs);
}
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);
}
Statement sta=StuFactory.getStatement();
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);
}
StuFactory.closeStu( sta, rs);
return s;

}
}





import java.sql.SQLException;
import java.util.ArrayList;


public class StuTest {

/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
    /* Stu s=new Stu();
     s.setName("d");
     StuDeal sdao=new StuDao();
     ArrayList <Stu>list=sdao.findCondition(s);
    for(int i=0;i<list.size();i++){
    System.out.println("学生id为:"+list.get(i).getId()+"学生姓名为:"+list.get(i).getName());
    }*/
StuDeal sdao=new StuDao();
Stu s=new Stu(11,"c");
sdao.insert(s);


}

}

你可能感兴趣的:(java,sql,mysql,jdbc)