为了账号安全,请及时绑定邮箱和手机立即绑定

List无法获取query方法,为什么??以下是我几个类的代码!麻烦大神看看有没改正和改进的地方

List无法获取query方法,为什么??以下是我几个类的代码!麻烦大神看看有没改正和改进的地方

Calvin1 2016-06-18 23:32:13
获取类package Affairs;public class Student { private String 教师姓名; private String 任课班级; private String 课程;  public Student() {  super();  // TODO Auto-generated constructor stub } public String get教师姓名() {  return 教师姓名; } public void set教师姓名(String 教师姓名) {  this.教师姓名 = 教师姓名; } public String get任课班级() {  return 任课班级; } public void set任课班级(String 任课班级) {  this.任课班级 = 任课班级; } public String get课程() {  return 课程; } public void set课程(String 课程) {  this.课程 = 课程; } public Student(String 教师姓名, String 任课班级, String 课程) {  super();  this.教师姓名 = 教师姓名;  this.任课班级 = 任课班级;  this.课程 = 课程; } public static void main(String[] args) {  // TODO Auto-generated method stub }}sql执行语句类package Affairs;import java.sql.*;import java.util.Scanner;public class StudentsManage { public void insert(String 教师姓名, String 任课班级, String 课程) {  // 建立连接  Connection conn = StudentsDbDrivers.getConnection();  if (conn != null) {   // 在建立与数据库连接的基础上,一个向数据库发送要执行的SQL语句的方法   try {    Statement s = conn.createStatement();    String sql = "insert into 教师管理 values('" + 教师姓名 + "," + 任课班级      + "," + 课程 + "')";    System.out.println(sql);    System.out.println("添加成功");    s.executeUpdate(sql);   } catch (SQLException e) {    System.out.println("添加失败");   }   StudentsDbDrivers.closeConnection(conn);  } } public void delete(String 教师姓名) {  Connection conn = StudentsDbDrivers.getConnection();  if (conn != null) {   try {    Statement s = conn.createStatement();    String sql = "delete from 教师管理 where 教师姓名='" + 教师姓名 + "'";    System.out.println("sql");    s.executeUpdate(sql);    System.out.println("删除成功");   } catch (SQLException e) {    System.out.println("删除失败");   }   StudentsDbDrivers.closeConnection(conn);  } } public void update(String 教师姓名, String 教师姓名1, String 任课班级1, String 课程1) {  Connection conn = StudentsDbDrivers.getConnection();  if (conn != null) {   try {    Statement s = conn.createStatement();    String sql = "update 教师管理 set 教师姓名=‘“+教师姓名1+”’,任课班级=‘“+任课班级1+”’,课程='"      + 课程1 + "'";    s.executeUpdate(sql);    System.out.println("修改成功");   } catch (SQLException e) {    System.out.println("更新失败");   }  } } public void query() {  Connection conn = StudentsDbDrivers.getConnection();  if(conn!=null){   try {    Statement s=conn.createStatement();    String sql="select *from 教师管理 where 教师姓名=‘“+教师姓名+”’";    ResultSet rs=s.executeQuery(sql);    System.out.println(sql);    while(rs.next());    System.out.println(rs.getString("教师姓名")+"");    System.out.println(rs.getString("任课班级")+"");    System.out.println(rs.getString("课程")+"");       } catch (SQLException e) {    System.out.println("查找成功");   }   StudentsDbDrivers.closeConnection(conn);  } } public static void main(String[] args) {  StudentsManage s =new StudentsManage();  Scanner scan = new Scanner(System.in);  System.out.println("请输入你要添加的教师姓名,任课班级,课程");  String 教师姓名 =scan.next();  String 任课班级=scan.next();  String 课程 = scan.next();  s.insert(教师姓名, 任课班级, 课程);     }}界面类:package Affairs;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.List;import javax.swing.*;import javax.swing.table.DefaultTableModel;public class StudentsFrame extends JFrame implements ActionListener{  JLabel jl1, jl2, jl3, jl4, jl5; JTextField jtf1, jtf2, jtf3, jtf4, jtf5; JComboBox jcb; JButton jb1, jb2, jb3, jb4, jb5; JTable jtable; JPanel jp1, jp2, jp3; JScrollPane jsp; String items[] = { "教师管理", "课程管理", "排课管理", "学生管理" }; String titles[] = { "教师姓名", "任课班级", "课程" }; DefaultTableModel dtm; StudentsDbDrivers sdd; StudentsManage ssm; StudentsFrame() {  jl1 = new JLabel("教师姓名");  jl2 = new JLabel("任课班级");  jl3 = new JLabel("课程");  jtf1 = new JTextField(10);  jtf2 = new JTextField(10);  jtf3 = new JTextField(10);  jb1 = new JButton("添加");  jb2 = new JButton("删除");  jb3 = new JButton("修改");  //jb4 = new JButton("刷新");  jb4 = new JButton("查询");  jcb = new JComboBox(items);  dtm = new DefaultTableModel(titles, 0);  jtable = new JTable(dtm);  jsp = new JScrollPane(jtable);  jp1 = new JPanel();  jp2 = new JPanel();  jp3 = new JPanel();  sdd = new StudentsDbDrivers(); } void init() {  this.setTitle("教务管理系统");  this.setSize(800, 600);  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  this.setVisible(true);  this.setLayout(new GridLayout(2, 1));  jp1.add(jl1);  jp1.add(jtf1);  jp1.add(jl2);  jp1.add(jtf2);  jp1.add(jl3);  jp1.add(jtf3);  jp1.add(jcb);  jp2.add(jb1);  jp2.add(jb2);  jp2.add(jb3);  jp2.add(jb4);  //jp2.add(jb5);  jp3.add(jp1);  jp3.add(jp2);  this.add(jp3, BorderLayout.NORTH);  this.add(jsp, BorderLayout.CENTER);    jb1.addActionListener(this);  jb2.addActionListener(this);  jb3.addActionListener(this);  jb4.addActionListener(this); } private void inittable(){  List list=ssm.query();  for(int i=0;i<list.size();i++){   Student stu =(Student)list.get(i);   String str[]={stu.get教师姓名(),stu.get任课班级(),stu.get课程()};     }   } public static void main(String[] args) {  StudentsFrame sf = new StudentsFrame();  sf.init(); } @Override public void actionPerformed(ActionEvent e) {  if(e.getSource().equals(jb1)){   String 教师姓名=jtf1.getText();   String 任课班级=jtf2.getText();   String 课程=jtf3.getText();   ssm.insert(教师姓名, 任课班级, 课程);   String str[]={教师姓名, 任课班级, 课程};   dtm.addRow(str);  }else if(e.getSource().equals(jb2)){    }   }}
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 1584 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信