package mypackage;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;import com.mysql.jdbc.PreparedStatement;public class StudentPersistent { private static StudentPersistent sp;
private StudentPersistent() 
{
}
public static StudentPersistent getStudentPresistent()
{
if(sp==null)
{
sp=new StudentPersistent();
}
return sp;
    } public void insertStudent(Connection con,Student student) throws SQLException
{
PreparedStatement ps=(PreparedStatement) con.prepareStatement("insert into student(name,id,sex,age) values (?,?,?,?)");
ps.setString(1, student.getName());
ps.setString(2, student.getId());
ps.setString(3, student.getSex());
ps.setInt(4, student.getAge());
ps.executeUpdate();
con.close();
}
public  Student findStudent(Connection con,String findId) throws SQLException
{
PreparedStatement ps= (PreparedStatement) con.prepareStatement("select * form student where id=?");
 ps.setString(1,findId);
ResultSet rs= ps.executeQuery();提示有错误!!!!!!!!
String aname=rs.getString(1);
String aid=rs.getString(2);
String asex=rs.getString(3);
int aage=rs.getInt(4);
Student findStudent=new Student(aname,aid,asex,aage);
return findStudent;
}
}package mypackage;import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;public class SystemUi extends JFrame { /**
 * @param args
 */
private JTextField studentName = new JTextField();
private JTextField studentId = new JTextField();
private JTextField studentSex = new JTextField();
private JTextField studentAge = new JTextField();
private JButton button1=new JButton("确定");
private JButton button2=new JButton("退出 ");
private JButton button3= new JButton("查找学生");
public SystemUi(String title)
{
super(title);
Container container=this.getContentPane();
container.setLayout(new GridLayout(5,3));
container.add(new JLabel("学生姓名"));
container.add(studentName);
container.add(new JLabel());
container.add(new JLabel("学生学号"));
container.add(studentId);
container.add(button3);
container.add(new JLabel("学生性别"));
container.add(studentSex);
container.add(new JLabel());
container.add(new JLabel("学生年龄"));
container.add(studentAge);
container.add(new JLabel());
container.add(button1);
container.add(button2);

this.setSize(300,300);
this.setVisible(true);
button1.addActionListener(new Listener1());
button2.addActionListener(new Listener2());
button3.addActionListener(new Listener3());

}
public class Listener1 implements ActionListener
{ public void actionPerformed(ActionEvent arg0){
// TODO Auto-generated method stub


try {
 String aname=studentName.getText();
     String aid=studentId.getText();
     String asex=studentSex.getText();
     String aage=studentAge.getText();
     int p_age=0;
     if(aage!=null)
     {
    p_age=Integer.parseInt(aage);
     }
Student student=new Student(aname,aid,asex,p_age);
Connection con=GetConnection.DriverManager();
StudentPersistent sp=StudentPersistent.getStudentPresistent();
sp.insertStudent(con, student);
System.out.println("添加成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }

}
public class Listener2 implements ActionListener
{ public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("退出管理系统");
System.exit(0);

}

}
public class Listener3 implements ActionListener
{ public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try
{
String findId=studentId.getText();
Connection con=GetConnection.DriverManager();
StudentPersistent ps=StudentPersistent.getStudentPresistent();
Student aStudent=ps.findStudent(con, findId);提示有错误!!!
studentName.setText(aStudent.getName());
studentSex.setText(aStudent.getSex());
int fage=aStudent.getAge();
String p_age=String.valueOf(fage);
studentAge.setText(p_age);

}
catch(Exception e1)
{
e1.printStackTrace();
}
}

}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SystemUi("学生管理系统"); }}

解决方案 »

  1.   

    package mypackage;import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;import com.mysql.jdbc.PreparedStatement;public class StudentPersistent {private static StudentPersistent sp;
    private StudentPersistent() 
    {
    }
    public static StudentPersistent getStudentPresistent()
    {
    if(sp==null)
    {
    sp=new StudentPersistent();
    }
    return sp;
    }public void insertStudent(Connection con,Student student) throws SQLException
    {
    PreparedStatement ps=(PreparedStatement) con.prepareStatement("insert into student(name,id,sex,age) values (?,?,?,?)");
    ps.setString(1, student.getName());
    ps.setString(2, student.getId());
    ps.setString(3, student.getSex());
    ps.setInt(4, student.getAge());
    ps.executeUpdate();
    con.close();
    }
    public Student findStudent(Connection con,String findId) throws SQLException
    {
    PreparedStatement ps= (PreparedStatement) con.prepareStatement("select * form student where id=?");
    ps.setString(1,findId);
    ResultSet rs= ps.executeQuery();提示有错误!!!!!!!!
    String aname=rs.getString(1);
    String aid=rs.getString(2);
    String asex=rs.getString(3);
    int aage=rs.getInt(4);
    Student findStudent=new Student(aname,aid,asex,aage);
    return findStudent;
    }
    }package mypackage;import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.SQLException;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;public class SystemUi extends JFrame {/**
    * @param args
    */
    private JTextField studentName = new JTextField();
    private JTextField studentId = new JTextField();
    private JTextField studentSex = new JTextField();
    private JTextField studentAge = new JTextField();
    private JButton button1=new JButton("确定");
    private JButton button2=new JButton("退出 ");
    private JButton button3= new JButton("查找学生");
    public SystemUi(String title)
    {
    super(title);
    Container container=this.getContentPane();
    container.setLayout(new GridLayout(5,3));
    container.add(new JLabel("学生姓名"));
    container.add(studentName);
    container.add(new JLabel());
    container.add(new JLabel("学生学号"));
    container.add(studentId);
    container.add(button3);
    container.add(new JLabel("学生性别"));
    container.add(studentSex);
    container.add(new JLabel());
    container.add(new JLabel("学生年龄"));
    container.add(studentAge);
    container.add(new JLabel());
    container.add(button1);
    container.add(button2);this.setSize(300,300);
    this.setVisible(true);
    button1.addActionListener(new Listener1());
    button2.addActionListener(new Listener2());
    button3.addActionListener(new Listener3());}
    public class Listener1 implements ActionListener
    {public void actionPerformed(ActionEvent arg0){
    // TODO Auto-generated method stub
    try {
    String aname=studentName.getText();
    String aid=studentId.getText();
    String asex=studentSex.getText();
    String aage=studentAge.getText();
    int p_age=0;
    if(aage!=null)
    {
    p_age=Integer.parseInt(aage);
    }
    Student student=new Student(aname,aid,asex,p_age);
    Connection con=GetConnection.DriverManager();
    StudentPersistent sp=StudentPersistent.getStudentPresistent();
    sp.insertStudent(con, student);
    System.out.println("添加成功");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }}}
    public class Listener2 implements ActionListener
    {public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    System.out.println("退出管理系统");
    System.exit(0);}}
    public class Listener3 implements ActionListener
    {public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    try
    {
    String findId=studentId.getText();
    Connection con=GetConnection.DriverManager();
    StudentPersistent ps=StudentPersistent.getStudentPresistent();
    Student aStudent=ps.findStudent(con, findId);提示有错误!!!
    studentName.setText(aStudent.getName());
    studentSex.setText(aStudent.getSex());
    int fage=aStudent.getAge();
    String p_age=String.valueOf(fage);
    studentAge.setText(p_age);}
    catch(Exception e1)
    {
    e1.printStackTrace();
    }
    }}
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new SystemUi("学生管理系统");}}
      

  2.   

    这种多数是sql语句问题select * form student where id=?form?