把源码发上来,大家给我找找问题
最近准备再网页上操作数据库上内容
到时候我会把源码也发出来
大家试验的时候把我oracle数据库连接部分改下就可以了。
//源码面板部分
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class StudentFace extends Frame implements ActionListener,ItemListener { /**
 * @author likui
 */
private static final long serialVersionUID = -4305034560732149318L; // panels buttons list labels ;
Panel north = new Panel(); Panel nl = new Panel(); List ls = new List(20, false); Panel nr = new Panel(); Label l1 = new Label("Id"); TextField tf1 = new TextField("", 10); Label l2 = new Label("Name"); TextField tf2 = new TextField("", 10); Label l3 = new Label("Sex"); TextField tf3 = new TextField("", 10); Label l4 = new Label("Age"); TextField tf4 = new TextField("", 10); Label l5 = new Label("Dept"); TextField tf5 = new TextField("", 10); Panel south = new Panel(); Button b1 = new Button("Look"); Button b2 = new Button("change"); Button b3 = new Button("add"); Button b4 = new Button("del"); public StudentFace() {
super("StudentManager");
this.add(north, BorderLayout.NORTH);
north.setLayout(new BorderLayout());
north.add(nl, BorderLayout.CENTER);
ls.addActionListener(this);
nl.add(ls);
nr.setLayout(new GridLayout(10, 1));
north.add(nr, BorderLayout.EAST);
nr.add(l1);
nr.add(tf1);
nr.add(l2);
nr.add(tf2);
nr.add(l3);
nr.add(tf3);
nr.add(l4);
nr.add(tf4);
nr.add(l5);
nr.add(tf5);
this.add(south, BorderLayout.SOUTH);
south.add(b1);
b1.addActionListener(this);
south.add(b2);
b2.addActionListener(this);
south.add(b3);
b3.addActionListener(this);
south.add(b4);
b4.addActionListener(this);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.pack();
this.setVisible(true); } public void actionPerformed(ActionEvent e) {
Object bn=e.getSource();
if(bn==b1){
this.ls.removeAll();
StudentDAO sd = new StudentDAO();
java.util.List studentlist=sd.findAll();
for (int i = 0; i < studentlist.size(); i++) {
StudentDTO st = (StudentDTO) studentlist.get(i);
ls.add(st.toString());
}
}if(bn==b2){
StudentDAO sa = new StudentDAO();
StudentDTO sd = new StudentDTO();
sd.setId(Integer.parseInt(tf1.getText()));
sd.setDept(tf5.getText());
sd.setAge(Integer.parseInt(tf4.getText()));
sd.setSex(tf3.getText());
sd.setName(tf2.getText());
sa.updateStudent(sd);
ls.replaceItem(sd.toString(),ls.getSelectedIndex());
}if(bn==b3){
StudentDAO sd = new StudentDAO();
StudentDTO st = new StudentDTO();
st.setName(tf2.getText());
st.setId(Integer.parseInt(tf1.getText()));
st.setDept(tf5.getText());
st.setAge(Integer.parseInt(tf4.getText()));
st.setSex(tf3.getText());
sd.addStudent(st);
ls.add(st.toString());
}if(bn==b4){
StudentDAO sa = new StudentDAO();
sa.delStudent(Integer.parseInt(tf1.getText()));
ls.remove(ls.getSelectedIndex());
}
}
public static void main(String[] args) {
new StudentFace();
} public void itemStateChanged(ItemEvent e) {
Integer s = (Integer)e.getItem();
String str = ls.getItem(s.intValue());
String[] ss = str.split(" ");
tf1.setText(ss[0]);
tf2.setText(ss[1]);
tf3.setText(ss[2]);
tf4.setText(ss[3]);
tf5.setText(ss[4]);

}
}

解决方案 »

  1.   

    //学生管理系统part 2
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;public class DBConnection {
    public static Connection conn=null;
    public static Statement stmt=null;
    ResultSet rs = null;

    public static Connection getConn(){
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection(
    "jdbc:oracle:thin:@localhost:1521:likui", "scott", "tiger");
    stmt=conn.createStatement();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return conn;
    }

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

  2.   

    //学生管理系统 part3
    import java.io.Serializable;
    public class StudentDTO implements Serializable{
    /**
     * @author likui
     */
    private static final long serialVersionUID = -4647007806419199349L;
    private int student_id ;
    private int age ;
    private String name ;
    private String dept ;
    private String sex;

    public int getAge(){
    return age ;
    }
    public void setAge(int age){
    this.age=age ;
    }

    public int getId(){
    return student_id ;
    }
    public void setId(int student_id){
    this.student_id=student_id ;
    }

    public String getName(){
    return name ;
    }
    public void setName(String name){
    this.name=name ;
    }
    public String getSex(){
    return sex ;
    }
    public void setSex(String sex){
    this.sex=sex ;
    }
    public String getDept(){
    return dept ;
    }
    public void setDept(String dept){
    this.dept=dept ;
    }
    public String toString(){
    String str = student_id+" "+name+" "+age+" "+dept;
    return str ;
    }
    }

      

  3.   

    //学生管理系统 part 4
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;public class StudentDAO {
    public List findAll(){
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    List list = new ArrayList();
    try{
    conn=DBConnection.getConn();
    stmt=conn.createStatement();
    rs = stmt.executeQuery("select s.*  from student s order by student_id");
    while (rs.next()) {
    StudentDTO sd = new StudentDTO();
    sd.setId(rs.getInt("student_id"));
    sd.setName(rs.getString("name"));
    sd.setSex(rs.getString("sex"));
    sd.setAge(rs.getInt("age"));
    sd.setDept(rs.getString("dept"));
    list.add(sd);
    }
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    DBConnection.close(rs);
    DBConnection.close(stmt);
    DBConnection.close(conn);
    }
    return list ;
    }
    public void addStudent(StudentDTO s){
    Connection conn=null ;
    PreparedStatement stmt=null ;
    try{
    conn=DBConnection.getConn();
    stmt=conn.prepareStatement("insert into student values(?,?,?,?)");
    stmt.setInt(1,s.getId());
    stmt.setString(2, s.getName());
    stmt.setString(3, s.getSex());
    stmt.setInt(4, s.getAge());
    stmt.setString(5, s.getDept());
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    DBConnection.close(stmt);
    DBConnection.close(conn);
    }
    }
    public void delStudent(int Id) {
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
    conn = DBConnection.getConn();
    stmt = conn.prepareStatement("delete student where studentid=?");
    stmt.setInt(1,Id);
    stmt.executeUpdate();
    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    DBConnection.close(stmt);
    DBConnection.close(conn);
    }
    }
    public void updateStudent(StudentDTO sd) {
    Connection conn = null;
    PreparedStatement stmt = null;

    try {
    conn = DBConnection.getConn();
    stmt = conn.prepareStatement("update student set name=?,age=?,dept=?,stime=to_date(?,'yyyy-mm-dd') where studentid=?");
    stmt.setInt(1,sd.getId());
    stmt.setString(2,sd.getName());
    stmt.setString(3,sd.getSex());
    stmt.setInt(4,sd.getAge());
    stmt.setString(5, sd.getDept());
    stmt.executeUpdate();
    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    DBConnection.close(stmt);
    DBConnection.close(conn);
    }
    }
    }
      

  4.   

    这代码应该不是你写的,应该也是从 CSDN 复制下来的!
      

  5.   

    package practice;
    import java.io.*;public class TestFile {
    public static void main(String[] args)
    {
    File path1 = new File("F:\\java\\practice\\src\\practice");
    File file1 = new File("F:\\java\\practice\\src\\practice\\file1");


    path1.mkdir();
    try
    {
    System.out.println("the path is being Created");
    file1.createNewFile();
    }
    catch(IOException ex)
    {
    ex.printStackTrace();
    }
    System.out.println("the name of the content :"+file1.getAbsolutePath());
    System.out.println("the content in the content is:");
    String[] sa = new File(".").list();      //这个'.'到底是什么,和这么用这个file类的list方法
    for(int i = 0; i< sa.length; i++)
    {
    System.out.println(sa[i]);
    }

    System.out.println("\n***************************\n");
    System.out.println("the content is deleted");

    file1.delete();
    path1.delete();
     
    sa = new File(".").list();
    for(int i = 0; i< sa.length; i++)
    {
    System.out.println(sa[i]);
    }




    }
    }
    这个中的list方法中的'.'到底是怎么回事啊!