当前位置:编程学习 > JAVA >>

java 与数据库的问题??


import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
import java.util.*;
class chaxun {
JButton bianhao;
JTextField bianhao1;
JLabel bianhao2;
AbstractTableModel tm;
Connection connection=null;
Statement statement=null;
ResultSet rs=null;
String title[]={"编号","姓名","性别","工资"};
Vector vector=new Vector();
AbstractTableModel tm;
JTable table;
JScrollPane scroll;
public chaxun(){
JFrame d=new JFrame("员工管理表");
container c=getContentpanetPane();
c.setLayout(new FlowLayout());
bianhao2=new JLabel("请输入查询关键词:");
bianhao1=new JTextField();
bianhao1.addActionListener(new Handler());
bianhao=new JButton("确定");
bianhao.addActionLIstener(new Handler());
c.add(bianhao2);
c.add(bianhao1);
c.add(bianhao);
c.setSize(500,500);
show();
}
private class Handler implements ActionListener{
public void actionPerFormed(ActionEvent e){
String s=" ";
if(e.getSoure==bianhao){
initTable();
s=bianhao1.gettext();
Frame f=new Frame("员工管理表");
f.setSize(500,500);
f.setLayout(new FlowLayout());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection("jdbc:odbc:mddb","li","234");
Statement stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery("select * from employee where 姓名=s or 性别=s or 工资=s");
vector.removeAllElements();
tm.fireTableStructureChanged();

while(rs.next()){
Vector vector1=new Vector();
vector1.addElement(String.valueOf(rs.getString("编号")));
vector1.addElement(rs.getString("姓名"));
vector1.addElement(rs.getString("性别"));
vector1.addElement(String.valueOf(rs.getInt("工资")));
vector.addElement(vector1);

}
}
}
public void initTable(){
tm=new AbstractTableModel(){
public int getColumnCount(){
return title.length;
}
public int getRowCount(){
return vector.size();
}
public Object getValueAt(int row,int column){
if(!vector.isEmpty()){
return ((Vector) vector.elementAt(row)).elementAt(column);
}else{
return null;
}
}
public void setValueAt(Object value,int row,int column){

}
public String getColumnName(int column){
return title[column];
}
public Class getColumnClass(int c){
return getValueAt(0,c).getClass();
}
public boolean isCellEditable(int row,int column){
return false;

}
};
table=new JTable(tm);

scroll=new JScrollPane(table);
scroll.setBounds(21,10,619,200);

f.add(scroll);

}
public static void main(String args[]){
chaxun t=new chaxun();
}

}

有什么错误啊

答案:您的程序错误不少,无法编译:
1. 重复定义AbstractTableModel tm;
2. container不知道是什么类型,getContentpanetPane()也没有定义:container c=getContentpanetPane();
3. addActionLIstener(new Handler()) 没有定义: bianhao.addActionLIstener(new Handler());
从2&3看来,您是想做GUI,那么chaxun需要或者extends JFrame,或者定义一个JFrame的全局变量, 然后您需要输入正确拼写的class以及方法名称(比如Container,getContentPane(),和 addActionListener(new Handler()))。之后您才可以使用诸如show()之类的方法(再插一句,show()方法已经淘汰,最好改为setVisible(true))。
4. 您让private class Handler implements ActionListener,但是又把必须实例化所有需要的方法(actionPerformed)拼错了(f不应该大写)。
5. 您的Handler class的边境({})配对不全。
6. e.getSoure 不知道是什么东西,假定您想写的是e.getSource()
7. 同样,bianhao1.gettext() 应该是bianhao1.getText()
8. f.add(scroll); 中的“f”没有定义,从上下文看,f 是Handler中的一个局部变量,在这里是读不到的。在Constructor中您有一句 JFrame d=new JFrame("Employee Manager"),d没有用处。应该是把 f 声明为JFrame的全局变量,再把这一句改成 =new JFrame("Employee Manager");同时把Handler中的初始删去。然后,删除关于Container c 的定义,改成用f。
9. 把这些都改正之后,还需要在和数据库连接的程序段加上try/catch,才可以编译。
既然都帮您改过了,不如把改好的程序贴出来。现在可以运行显示JFrame。至于数据功能,连不到您的数据库,无法帮您测试。因为我的IDE不支持中文,所以把您的标签之类改成了英文,请您自己改回去:
package com.pfizer.rtc.usage;
import java.sql.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.table.*;import java.io.*;import java.util.*;class chaxun { JFrame f;JButton bianhao;JTextField bianhao1;JLabel bianhao2;//AbstractTableModel tm;Connection connection=null;Statement statement=null;ResultSet rs=null;String title[]={"ID","Name","Sex","Salary"};Vector vector=new Vector();AbstractTableModel tm;JTable table;JScrollPane scroll;public chaxun (){//JFrame d=new JFrame("Employee Manager");f=new JFrame("Employee Manager");//Container c= getContentPane();f.setLayout(new FlowLayout());bianhao2=new JLabel("Please Input Search Key:");bianhao1=new JTextField();bianhao1.addActionListener(new Handler());bianhao=new JButton("Confirm");bianhao.addActionListener(new Handler());f.add(bianhao2);f.add(bianhao1);f.add(bianhao);f.setSize(500,500);f.setVisible(true);//show();}private class Handler implements ActionListener{public void actionPerformed(ActionEvent e){String s=" ";if(e.getSource()==bianhao){initTable();s=bianhao1.getText();//Frame f=new Frame("Employee Manager");f.setSize(500,500);f.setLayout(new FlowLayout());try {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");connection=DriverManager.getConnection("jdbc:odbc:mddb","li","234");Statement stmt=connection.createStatement();ResultSet rs=stmt.executeQuery("select * from employee where Name=s or Sex=s or Salary=s");vector.removeAllElements();tm.fireTableStructureChanged();
while(rs.next()){Vector vector1=new Vector();vector1.addElement(String.valueOf(rs.getString("ID")));vector1.addElement(rs.getString("Name"));vector1.addElement(rs.getString("Sex"));vector1.addElement(String.valueOf(rs.getInt("Salaary")));vector.addElement(vector1);
}} catch (ClassNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}}}public void initTable(){ tm=new AbstractTableModel(){public int getColumnCount(){return title.length;}public int getRowCount(){return vector.size();}public Object getValueAt(int row,int column){if(!vector.isEmpty()){return ((Vector) vector.elementAt(row)).elementAt(column);}else{return null;}}public void setValueAt(Object value,int row,int column){
}public String getColumnName(int column){return title[column];}public Class getColumnClass(int c){return getValueAt(0,c).getClass();}public boolean isCellEditable(int row,int column){return false;
}};table=new JTable(tm);scroll=new JScrollPane(table);scroll.setBounds(21,10,619,200);
f.add(scroll);
}public static void main(String args[]){chaxun t=new chaxun();}}

最后建议您以后写程序的时候稍微细心一些,或者采用IDE帮助自己避免很多这里指出的错误。

public void actionPerFormed(ActionEvent e) ?的class。forname

?connection=DriverManager.getConnection("jdbc:odbc:mddb","li","234");

Statement stmt=connection.createStatement();

没有try catch ? 、方法也没 throws 个异常 ? ?是不是 那个原因啊?


上一个:求一个java编程的计算器程序
下一个:帮忙看看这个java程序有什么问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,