給個類似的例子妳(學生 的增刪查改,數據庫MySQL,絕對可以運行,如果妳是sql Server 2000數據庫的話,就是改下數據庫驅動的語句就行了)
學生類(封裝學生的信息)
package com.dto;
public class StudentDTO
{
/*定義屬性與相應的表中的列名對應*/
private int stuNum;
private String stuName;
private int stuAge;
private String stuSex;
/*生成兩個構造器:壹個無參,壹個全部參數*/
public StudentDTO() {
super();
}
public StudentDTO(int stuNum, String stuName, int stuAge, String stuSex) {
super();
this.stuNum = stuNum;
this.stuName = stuName;
this.stuAge = stuAge;
this.stuSex = stuSex;
}
/*生成Get/Set器*/
public int getStuNum() {
return stuNum;
}
public void setStuNum(int stuNum) {
this.stuNum = stuNum;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
}
操作類(封裝具體的操作)
package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.dto.StudentDTO;
/*DAO用來封裝java針對於數據庫的各種操作*/
public class JDBCDAO
{
/*獲得所有的記錄*/
public List getAll()
{
Connection conn=null;
List<StudentDTO> students=new ArrayList<StudentDTO>();
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
PreparedStatement ps=conn.prepareStatement("select stuNum,stuName,stuAge,stuSex from student");
/*可以替換為:*/
// PreparedStatement ps=conn.prepareStatement("select * from student");
ResultSet rs=ps.executeQuery();
/*重點*/
while(rs.next())
{
StudentDTO student=new StudentDTO();
student.setStuNum(rs.getInt("stuNum"));
student.setStuName(rs.getString("stuName"));
student.setStuAge(rs.getInt("stuAge"));
student.setStuSex(rs.getString("stuSex"));
students.add(student);
}
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
return students;
}
/*插入壹條記錄*/
public void addStudent(StudentDTO student)
{
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
/*重點*/
PreparedStatement ps=conn.prepareStatement("INSERT into student values (null,?,?,?)");
/*相對應的?按照出現順序賦值
* 參數1,2,3代表上面問號的位置
* 說明:對第壹個問號傳壹個學生姓名
* 對第二個問號傳壹個學生年齡
* 對第三個問號傳壹個學生性別*/
ps.setString(1,student.getStuName());
ps.setInt(2,student.getStuAge());
ps.setString(3,student.getStuSex());
ps.execute();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
/*更新壹條記錄*/
public void upStudent(StudentDTO student)
{
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
/*重點*/
PreparedStatement ps=conn.prepareStatement("update student set stuName=?,stuAge=?,stuSex=? where stuNum=?");
/*相對應的?按照出現順序賦值
* 參數1,2,3代表上面問號的位置
* 說明:對第壹個問號傳壹個學生姓名
* 對第二個問號傳壹個學生年齡
* 對第三個問號傳壹個學生性別
* 對第四個問號傳壹個學生學號*/
ps.setString(1,student.getStuName());
ps.setInt(2,student.getStuAge());
ps.setString(3,student.getStuSex());
ps.setInt(4,student.getStuNum());
ps.execute();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
/*刪除壹條記錄*/
public void deleteStudent(int stuNum)
{
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","root","1234");
/*重點*/
PreparedStatement ps=conn.prepareStatement("delete from student where stuNum=?");
/*相對應的?按照出現順序賦值
* 參數1代表上面問號的位置
* 說明:對stuNum傳壹個int類型的值*/
ps.setInt(1,stuNum);
ps.execute();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
if(conn!=null)
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
/*main裏面的測試代碼壹個壹個的執行*/
public static void main(String[] args)
{
/*測試getAll()方法代碼*/
// List<StudentDTO> students=new JDBCDAO().getAll();
// for(StudentDTO student:students)
// {
// System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
// }
/*測試addStudent()方法代碼*/
// StudentDTO dto=new StudentDTO();
// dto.setStuName("劉菲");
// dto.setStuAge(18);
// dto.setStuSex("女");
// new JDBCDAO().addStudent(dto);
// /*添加後獲得所有數據*/
// List<StudentDTO> students=new JDBCDAO().getAll();
// for(StudentDTO student:students)
// {
// System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
// }
/*測試upStudent()方法代碼*/
// StudentDTO dto=new StudentDTO();
// dto.setStuNum(1);
// dto.setStuName("牛牛羊");
// dto.setStuAge(18);
// dto.setStuSex("男");
// new JDBCDAO().upStudent(dto);
// /*更新後獲得所有數據*/
// List<StudentDTO> students=new JDBCDAO().getAll();
// for(StudentDTO student:students)
// {
// System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
// }
/*測試deleteStudent()方法代碼*/
new JDBCDAO().deleteStudent(6);
List<StudentDTO> students=new JDBCDAO().getAll();
for(StudentDTO student:students)
{
System.out.println(student.getStuNum()+":"+student.getStuName()+":"+student.getStuAge()+":"+student.getStuSex());
}
}
}