这个程序是个作业,好久还没写好:
1:Vector Info 构造函数中的Info始终是空的,按不同的按钮之后,table出现的效果太差。
2:addData()等函数无法读取JTextField中的值。编译时报告未初始化。每次使用addTata()时总是要出现两行。
3:代码中应该有check函数判断输入的值是否符合要求,并抛出异常,但是编译时总提示(check函数相当于伪代码);
4:无法save,不知道是语句的问题,还是连接数据库有问题。
5:单击一个JTextField,再按tab下一个JTextField无法清空,我想实现,但还是处理完问题。
6:连接数据库的方式是不是用Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testDB"; String user = "sa"; String password = "test"; Connection conn = DriverManager.getConnection(url, user, password); 要好点。
7:代码看起来太多了。
8:请大家教授一下吧,谢谢大家了。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;
import java.util.*;
import java.io.*;
import java.sql.*;
import java.awt.Graphics;public class ManageScore extends JFrame implements ActionListener{
/***********初始化*****************************/
private JFrame frame;
private JButton record,search,save,searchall;
private JLabel name1,name2,course1,course2,score,title,bottom;
private JTextField tname1,tname2,tcourse1,tcourse2,tscore;
DefaultTableModel dmt;
private JTable table;
//JTextArea area;
private JScrollPane areascroll;
private JPanel panel1,panel2;
Graphics g;
public static Vector Info;
Vector tempInfo;
Vector Names;
Object name[]={"姓名","课程","得分"};
Connection con;
Statement sql;
ResultSet rs; private Exception scoreException;
private Exception courseException;
private Exception searchException;
private Exception recordException;
/*****************构造函数*******************/
ManageScore()
{
/****************JFrame**********************/
frame = new JFrame("分数管理器");
frame.setFont(new Font("微软雅黑",Font.PLAIN,12));
ImageIcon icon=new ImageIcon("D:\\52.png");//要16X16的图片
frame.setIconImage(icon.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭按钮的动作为退出窗口
//frame.setSize(100, 100);// 设置窗口大小
Dimension displaySize = Toolkit.getDefaultToolkit().getScreenSize();// 获得显示器大小对象
Dimension frameSize = frame.getSize();// 获得窗口大小对象
if (frameSize.width > displaySize.width)
frameSize.width = displaySize.width;// 窗口的宽度不能大于显示器的宽度
if (frameSize.height > displaySize.height)
frameSize.height = displaySize.height;// 窗口的高度不能大于显示器的高度
frame.setLocation((displaySize.width - frameSize.width) / 7,
(displaySize.height - frameSize.height) / 7);// 设置窗口居中显示器显示
/****************JTable************************/
Info = new Vector();
Names = new Vector(Arrays.asList(name));
dmt=new DefaultTableModel(Info,Names){
public boolean isCellEditable(int row, int column) {
return false;
}
};
table=new JTable(dmt);
searchAllData();
/******************JPanel***************************/
panel1 = new JPanel();
//panel1.setSize(1000,1000);
panel1.setFont(new Font("微软雅黑",Font.PLAIN,12));
panel1.setLayout(new GridLayout(2,10,5,5));
panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory
.createLineBorder(Color.BLUE), "操作"));
panel2 = new JPanel();
panel2.setLayout(new BorderLayout(5,5));
name1 = new JLabel("姓名: ");
name1.setHorizontalAlignment(JLabel.RIGHT);
name2 = new JLabel("姓名: ");
name2.setHorizontalAlignment(JLabel.RIGHT);
course1 = new JLabel("课程: ");
course1.setHorizontalAlignment(JLabel.RIGHT);
course2 = new JLabel("课程: ");
course2.setHorizontalAlignment(JLabel.RIGHT);
score = new JLabel("分数: ");
score.setHorizontalAlignment(JLabel.RIGHT);
record = new JButton("录入");
record.addActionListener(this);
record.setCursor(new Cursor(getCursor().HAND_CURSOR));
search = new JButton("查找");
search.addActionListener(this);
search.setCursor(new Cursor(getCursor().HAND_CURSOR));
save = new JButton("保存");
save.addActionListener(this);
save.setCursor(new Cursor(getCursor().HAND_CURSOR));
searchall = new JButton("查询所有");
searchall.addActionListener(this);
searchall.setCursor(new Cursor(getCursor().HAND_CURSOR));
tname1 = new JTextField(4);
tname2 = new JTextField(4);
tcourse1 = new JTextField(10);
tcourse2 = new JTextField(10);
tscore = new JTextField(3);
tname1.setText("姓名");
tcourse1.setText("课程");
tscore.setText("分数");
tname2.setText("姓名");
tcourse2.setText("课程");
tname1.setBackground(new Color(238,238,238));
tcourse1.setBackground(new Color(238,238,238));
tscore.setBackground(new Color(238,238,238));
tname2.setBackground(new Color(238,238,238));
tcourse2.setBackground(new Color(238,238,238));
tname1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tname1.setBackground(Color.white);
tname1.setText(null);
}});
tname1.addKeyListener(new KeyAdapter(){
public void KeyPressed(KeyEvent e){
if(e.getKeyCode()==10){
//tname1.setBackground(Color.white);
//tname1.setText(null);
record.doClick();
}
if(e.getKeyCode()==9)
{ tcourse1.setFocusable(true);
tname2.setBackground(Color.white);
tname2.setText(null);}
}});
tname2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tname2.setBackground(Color.white);
tname2.setText(null);
}});
tcourse1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tcourse1.setBackground(Color.white);
tcourse1.setText(null);
}});
tcourse2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tcourse2.setBackground(Color.white);
tcourse2.setText(null);
} });
tscore.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tscore.setBackground(Color.white);
tscore.setText(null);
}});
panel1.add(record);
panel1.add(name1);
panel1.add(tname1);
panel1.add(course1);
panel1.add(tcourse1);
panel1.add(score);
panel1.add(tscore);
panel1.add(search);
panel1.add(name2);
panel1.add(tname2);
panel1.add(course2);
panel1.add(tcourse2);
panel1.add(save);
panel1.add(searchall);
table = new JTable(dmt);
table.getTableHeader().setReorderingAllowed(false);
table.setSelectionBackground(Color.white);
table.clearSelection();
table.setBackground(new Color(157,217,234));
table.setPreferredScrollableViewportSize(new Dimension(820,250));
areascroll = new JScrollPane(table);
title = new JLabel(" ");
title.setPreferredSize(new Dimension(820,5));
bottom = new JLabel("");
bottom.setPreferredSize(new Dimension(820,5));
panel2.add(title,BorderLayout.NORTH);
panel2.add(areascroll,BorderLayout.WEST);
panel2.add(bottom,BorderLayout.SOUTH);
frame.add(panel1,BorderLayout.NORTH);
frame.add(panel2,BorderLayout.SOUTH);
record.addActionListener(this);
search.addActionListener(this);
frame.pack();
frame.setVisible(true);
validate();
}
/******************Main***************************/
public static void main(String args[]) throws Exception{
new ManageScore();
}
/******************Action***************************/
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
if(comm.equals("录入")){//记录事件
addData();
}
if(comm.equals("查找")){//查询事件
searchData();
Info.removeAllElements();
}
if(comm.equals("保存")){//
save();
}
if(comm.equals("查询所有")){//查询所有
searchAllData();
// Info.removeAllElements();
}
}
/*****************check****************************/
/*void checkcourse(String course) throws courseException{
if(course=="")
throw courseException;
}
void checkscore(String score) throws scoreException{
int num = Integer.parseInt(score);
if(num>100||num<0)
throw scoreException;
}
void checkrecord(String name,String course,String score) throws searchException{
if(name==""||course==""||score=="")
throw searchException;
}
void checksearch(String name,String course) throws recordException{
if(name==""||course=="")
throw recordException;
}
1:Vector Info 构造函数中的Info始终是空的,按不同的按钮之后,table出现的效果太差。
2:addData()等函数无法读取JTextField中的值。编译时报告未初始化。每次使用addTata()时总是要出现两行。
3:代码中应该有check函数判断输入的值是否符合要求,并抛出异常,但是编译时总提示(check函数相当于伪代码);
4:无法save,不知道是语句的问题,还是连接数据库有问题。
5:单击一个JTextField,再按tab下一个JTextField无法清空,我想实现,但还是处理完问题。
6:连接数据库的方式是不是用Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testDB"; String user = "sa"; String password = "test"; Connection conn = DriverManager.getConnection(url, user, password); 要好点。
7:代码看起来太多了。
8:请大家教授一下吧,谢谢大家了。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;
import java.util.*;
import java.io.*;
import java.sql.*;
import java.awt.Graphics;public class ManageScore extends JFrame implements ActionListener{
/***********初始化*****************************/
private JFrame frame;
private JButton record,search,save,searchall;
private JLabel name1,name2,course1,course2,score,title,bottom;
private JTextField tname1,tname2,tcourse1,tcourse2,tscore;
DefaultTableModel dmt;
private JTable table;
//JTextArea area;
private JScrollPane areascroll;
private JPanel panel1,panel2;
Graphics g;
public static Vector Info;
Vector tempInfo;
Vector Names;
Object name[]={"姓名","课程","得分"};
Connection con;
Statement sql;
ResultSet rs; private Exception scoreException;
private Exception courseException;
private Exception searchException;
private Exception recordException;
/*****************构造函数*******************/
ManageScore()
{
/****************JFrame**********************/
frame = new JFrame("分数管理器");
frame.setFont(new Font("微软雅黑",Font.PLAIN,12));
ImageIcon icon=new ImageIcon("D:\\52.png");//要16X16的图片
frame.setIconImage(icon.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭按钮的动作为退出窗口
//frame.setSize(100, 100);// 设置窗口大小
Dimension displaySize = Toolkit.getDefaultToolkit().getScreenSize();// 获得显示器大小对象
Dimension frameSize = frame.getSize();// 获得窗口大小对象
if (frameSize.width > displaySize.width)
frameSize.width = displaySize.width;// 窗口的宽度不能大于显示器的宽度
if (frameSize.height > displaySize.height)
frameSize.height = displaySize.height;// 窗口的高度不能大于显示器的高度
frame.setLocation((displaySize.width - frameSize.width) / 7,
(displaySize.height - frameSize.height) / 7);// 设置窗口居中显示器显示
/****************JTable************************/
Info = new Vector();
Names = new Vector(Arrays.asList(name));
dmt=new DefaultTableModel(Info,Names){
public boolean isCellEditable(int row, int column) {
return false;
}
};
table=new JTable(dmt);
searchAllData();
/******************JPanel***************************/
panel1 = new JPanel();
//panel1.setSize(1000,1000);
panel1.setFont(new Font("微软雅黑",Font.PLAIN,12));
panel1.setLayout(new GridLayout(2,10,5,5));
panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory
.createLineBorder(Color.BLUE), "操作"));
panel2 = new JPanel();
panel2.setLayout(new BorderLayout(5,5));
name1 = new JLabel("姓名: ");
name1.setHorizontalAlignment(JLabel.RIGHT);
name2 = new JLabel("姓名: ");
name2.setHorizontalAlignment(JLabel.RIGHT);
course1 = new JLabel("课程: ");
course1.setHorizontalAlignment(JLabel.RIGHT);
course2 = new JLabel("课程: ");
course2.setHorizontalAlignment(JLabel.RIGHT);
score = new JLabel("分数: ");
score.setHorizontalAlignment(JLabel.RIGHT);
record = new JButton("录入");
record.addActionListener(this);
record.setCursor(new Cursor(getCursor().HAND_CURSOR));
search = new JButton("查找");
search.addActionListener(this);
search.setCursor(new Cursor(getCursor().HAND_CURSOR));
save = new JButton("保存");
save.addActionListener(this);
save.setCursor(new Cursor(getCursor().HAND_CURSOR));
searchall = new JButton("查询所有");
searchall.addActionListener(this);
searchall.setCursor(new Cursor(getCursor().HAND_CURSOR));
tname1 = new JTextField(4);
tname2 = new JTextField(4);
tcourse1 = new JTextField(10);
tcourse2 = new JTextField(10);
tscore = new JTextField(3);
tname1.setText("姓名");
tcourse1.setText("课程");
tscore.setText("分数");
tname2.setText("姓名");
tcourse2.setText("课程");
tname1.setBackground(new Color(238,238,238));
tcourse1.setBackground(new Color(238,238,238));
tscore.setBackground(new Color(238,238,238));
tname2.setBackground(new Color(238,238,238));
tcourse2.setBackground(new Color(238,238,238));
tname1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tname1.setBackground(Color.white);
tname1.setText(null);
}});
tname1.addKeyListener(new KeyAdapter(){
public void KeyPressed(KeyEvent e){
if(e.getKeyCode()==10){
//tname1.setBackground(Color.white);
//tname1.setText(null);
record.doClick();
}
if(e.getKeyCode()==9)
{ tcourse1.setFocusable(true);
tname2.setBackground(Color.white);
tname2.setText(null);}
}});
tname2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tname2.setBackground(Color.white);
tname2.setText(null);
}});
tcourse1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tcourse1.setBackground(Color.white);
tcourse1.setText(null);
}});
tcourse2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tcourse2.setBackground(Color.white);
tcourse2.setText(null);
} });
tscore.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tscore.setBackground(Color.white);
tscore.setText(null);
}});
panel1.add(record);
panel1.add(name1);
panel1.add(tname1);
panel1.add(course1);
panel1.add(tcourse1);
panel1.add(score);
panel1.add(tscore);
panel1.add(search);
panel1.add(name2);
panel1.add(tname2);
panel1.add(course2);
panel1.add(tcourse2);
panel1.add(save);
panel1.add(searchall);
table = new JTable(dmt);
table.getTableHeader().setReorderingAllowed(false);
table.setSelectionBackground(Color.white);
table.clearSelection();
table.setBackground(new Color(157,217,234));
table.setPreferredScrollableViewportSize(new Dimension(820,250));
areascroll = new JScrollPane(table);
title = new JLabel(" ");
title.setPreferredSize(new Dimension(820,5));
bottom = new JLabel("");
bottom.setPreferredSize(new Dimension(820,5));
panel2.add(title,BorderLayout.NORTH);
panel2.add(areascroll,BorderLayout.WEST);
panel2.add(bottom,BorderLayout.SOUTH);
frame.add(panel1,BorderLayout.NORTH);
frame.add(panel2,BorderLayout.SOUTH);
record.addActionListener(this);
search.addActionListener(this);
frame.pack();
frame.setVisible(true);
validate();
}
/******************Main***************************/
public static void main(String args[]) throws Exception{
new ManageScore();
}
/******************Action***************************/
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
if(comm.equals("录入")){//记录事件
addData();
}
if(comm.equals("查找")){//查询事件
searchData();
Info.removeAllElements();
}
if(comm.equals("保存")){//
save();
}
if(comm.equals("查询所有")){//查询所有
searchAllData();
// Info.removeAllElements();
}
}
/*****************check****************************/
/*void checkcourse(String course) throws courseException{
if(course=="")
throw courseException;
}
void checkscore(String score) throws scoreException{
int num = Integer.parseInt(score);
if(num>100||num<0)
throw scoreException;
}
void checkrecord(String name,String course,String score) throws searchException{
if(name==""||course==""||score=="")
throw searchException;
}
void checksearch(String name,String course) throws recordException{
if(name==""||course=="")
throw recordException;
}
private void dataConnection() {
try
{
con=DriverManager.getConnection("jdbc:odbc:Student","","");
sql=con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=sql.executeQuery(" ");
rs.last();
}catch(SQLException sq5){}
}
private void addData(){
if(tname1.getText() == null||tcourse1.getText() == null||tscore.getText( )== null){
JOptionPane.showMessageDialog(frame, "!");System.exit(0);}
else if(tname1.getText( )== "姓名"||tcourse1.getText() == "课程"||tscore.getText() == "分数"){
JOptionPane.showMessageDialog(frame, "!");System.exit(0);}
else{
tempInfo = new Vector();
tempInfo.add(tname1.getText());
tempInfo.add(tcourse1.getText());
tempInfo.add(tscore.getText());
Info.add(tempInfo);
//table.addNotify();
table.updateUI();
tname1.setText("姓名");
tcourse1.setText("课程");
tscore.setText("分数");
}
tname1.setBackground(new Color(238,238,238));
tcourse1.setBackground(new Color(238,238,238));
tscore.setBackground(new Color(238,238,238));
}
private void searchData(){
try{
dataConnection();
dmt.setRowCount(0);
Info.removeAllElements();
String dia ="select * from ST";
if(tname2.getText()!=null&&tcourse2.getText()!=null&&tcourse2.getText()!="课程"){
dia = "Select * From ST " +
"Where Name='"+tname2.getText()+"' and Course='"+tcourse2.getText()+";";}
else if(tname2.getText()!=null&&(tcourse2.getText()==null||tcourse2.getText()=="课程")){
dia = "Select * From ST " +
"Where Name="+tname2.getText()+"';";}
else
JOptionPane.showMessageDialog(null, "!");
rs = sql.executeQuery(dia);
while(rs.next()){
tempInfo = new Vector();
tempInfo.add(rs.getString("Name"));
tempInfo.add(rs.getString("Course"));
tempInfo.add(rs.getString("Score"));
Info.add(tempInfo);
}
//dmt.fireTableStructureChanged();
//rs.refreshRow();
con.close();
table.addNotify();
tname2.setText("姓名");
tcourse2.setText("课程");
//table.repaint();
//table.updateUI();
//Info.removeAllElements();
}
catch(SQLException sq2){}
}
private void save(){
try{
dataConnection();
int count = Info.size();
int k=0;
while(k++<count){
String dia = "insert into ST(Name,Course,Score) values('"
+Info.get(k).toString().substring(1)+"','"
+Info.get(k).toString().substring(2)+"','"
+Info.get(k).toString().substring(3)+"');";
int st=sql.executeUpdate(dia);
}
con.close();
}catch(SQLException e){}
}
private void searchAllData(){
try{
dataConnection();
Info.removeAllElements();
rs = sql.executeQuery("SELECT * FROM ST");//加到集合前面
//Info = new Vector();
while(rs.next()){
tempInfo = new Vector();
tempInfo.add(rs.getString("Name"));
tempInfo.add(rs.getString("Course"));
tempInfo.add(rs.getString("Score"));
Info.add(tempInfo);
}
rs.close();
con.close();
table.addNotify();
}
catch(SQLException e){}}
}/******************异常 ***************************/
class courseException extends Exception{//课程异常
void courseException(JFrame jf){
JDialog jd = new JDialog(jf,"错误!!"); jd.setLayout(new FlowLayout());
JLabel jl = new JLabel("请认真填写。 ");
JLabel j2 = new JLabel("无此课程");
jd.add(jl);
jd.add(j2);
jd.setSize(180, 100);
jd.setResizable(false);
jd.setLocation(400, 240);
jd.setVisible(true);
}
}
class scoreException extends Exception{//分数异常
void scoreException(JFrame jf){
JDialog jd = new JDialog(jf,"错误!!"); jd.setLayout(new FlowLayout());
JLabel j = new JLabel("分数只能在0到100。 ");
jd.add(j);
jd.setSize(180, 100);
jd.setResizable(false);
jd.setLocation(400, 240);
jd.setVisible(true);
}
}
class searchException extends Exception{//查询异常
void searchException(JFrame jf){
JDialog jd = new JDialog(jf,"错误!!"); jd.setLayout(new FlowLayout());
JLabel j = new JLabel("未填完真,请检查。 ");
jd.add(j);
jd.setSize(180, 100);
jd.setResizable(false);
jd.setLocation(400, 240);
jd.setVisible(true);
}
}
class recordException extends Exception{//录入异常
void recordException(JFrame jf){
JDialog jd = new JDialog(jf,"错误!!");
jd.setLayout(new FlowLayout());
JLabel j = new JLabel("未填完整,请检查。 ");
jd.add(j);
jd.setSize(180, 100);
jd.setResizable(false);
jd.setLocation(400, 240);
jd.setVisible(true);
}
}[/code]
private static final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url="jdbc:sqlserver://localhost:1433;DataBaseName=BANK";
private static final String user="sa";
private static final String pwd ="123456";
private static Connection con=null;
private static ResultSet set=null;
private static Statement state =null;
public static Connection getCon()
{
try {
Class.forName(driver);
con=DriverManager.getConnection(url,user,pwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
//关闭Connection
public static void closeCon(Connection con)
{
try
{
if(con!=null)
{
con.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
//关闭ResultSet
public static void closeResultSet(ResultSet set)
{
try
{
if(set!=null)
{
set.close();
set=null;
}
}catch(Exception e)
{
e.printStackTrace();
}
}
//关闭Statement
public static void closeStatement(Statement state)
{
try
{
if(state!=null)
{
state.close();
state=null;
}
}catch(Exception e)
{
e.printStackTrace();
}
}