📄 queframe.java~49~
字号:
package ct;import javax.swing.*;import java.awt.*;import com.borland.jbcl.layout.*;import java.awt.event.*;import com.borland.dbswing.*;import com.borland.dx.sql.dataset.*;import java.sql.*;public class QueFrame extends JFrame { JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); XYLayout xYLayout1 = new XYLayout(); JTextField username = new JTextField(); JTextField score = new JTextField(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); /////////////////////////////////////////////////////////////// Connection conn=null;// Statement sttm=null; String sql=""; ResultSet rs=null; QueryDataSet queryDataSet1=new QueryDataSet(); Database dataBase1=new Database(); public QueFrame(int m_score) { try { jbInit(); score.setText(""+m_score); username.setText(""+); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { jLabel1.setFont(new java.awt.Font("Dialog", 0, 12)); jLabel1.setText("姓名:"); jLabel2.setFont(new java.awt.Font("Dialog", 0, 12)); jLabel2.setText("分数:"); this.getContentPane().setLayout(xYLayout1); username.setFont(new java.awt.Font("Dialog", 0, 12)); username.setEditable(false); username.setText(""); score.setEnabled(true); score.setFont(new java.awt.Font("Dialog", 0, 12)); score.setEditable(false); score.setText(""); jButton1.setText("进入"); jButton1.addActionListener(new QueFrame_jButton1_actionAdapter(this)); jButton2.setText("查看"); jButton2.addActionListener(new QueFrame_jButton2_actionAdapter(this)); // xYLayout1.setWidth(211); // xYLayout1.setHeight(195); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.setEnabled(true); this.setLocale(java.util.Locale.getDefault()); this.setResizable(true); this.setTitle("本轮得分"); this.setState(Frame.NORMAL); this.getContentPane().add(jLabel2, new XYConstraints(25, 90, 70, 30)); this.getContentPane().add(jLabel1, new XYConstraints(25, 40, 70, 30)); this.getContentPane().add(username, new XYConstraints(100, 40, 80, 30)); this.getContentPane().add(score, new XYConstraints(100, 90, 80, 30)); this.getContentPane().add(jButton1, new XYConstraints(20, 130, 70, 30)); this.getContentPane().add(jButton2, new XYConstraints(110, 130, 70, 30)); /////////////////////////////////// } public static void main(String args[]){ // QueFrame qf=new QueFrame(300); // qf.setSize(210,230); // qf.setLocation(450,200); // qf.show(); } void jButton1_actionPerformed(ActionEvent e) { /////////////////////////////////////// try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); try { conn=DriverManager.getConnection("jdbc:odbc:xqy"); sttm=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); } catch(SQLException eu) { System.out.println(eu.getMessage()); } } catch(ClassNotFoundException ey) { System.out.println(ey.getMessage()); }///////////////////////////////////////////////////// try { sql="select * from paihangbang where name='"+username.getText().trim()+"'";//判断此用户是否已经存在于本数据库中 rs=sttm.executeQuery(sql); rs.absolute(1); if(!rs.isFirst())//如果不存在此用户,则插入数据 { sql="insert into paihangbang values ('"+username.getText().trim()+"','"+score.getText().trim()+"')"; int i=sttm.executeUpdate(sql); if(i==1) { JOptionPane.showConfirmDialog(null,"成功进入排行榜","恭喜",JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE); dispose(); } else { JOptionPane.showConfirmDialog(null,"进入成功排行榜","提示",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE); } } else//如果此用户已经存在,则更新用户的成绩 { int newscore=Integer.parseInt(score.getText().toString()); int oldscore=rs.getInt(2); System.out.println(score); if(oldscore<newscore)//当用户已经存在是,我们要判断此次分数是否比原来的分数高 { sql = "update paihangbang set score='" + newscore + "'where name='" + username.getText().trim() + "'"; int i = sttm.executeUpdate(sql); if (i == 1) { JOptionPane.showConfirmDialog(null, "成功进入排行榜", "恭喜", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE); dispose(); } else { JOptionPane.showConfirmDialog(null, "进入成功排行榜", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE); } } else { JOptionPane.showConfirmDialog(null, "还要继续加油哦", "加油", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE); } } } catch(SQLException ee) { System.out.println(ee.getMessage()); } try { conn.close(); sttm.close(); rs.close(); } catch(SQLException er) { System.out.println(er.getMessage()); } } void jButton2_actionPerformed(ActionEvent e) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); try { conn=DriverManager.getConnection("jdbc:odbc:xqy"); sttm=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); } catch(SQLException eu) { System.out.println(eu.getMessage()); } } catch(ClassNotFoundException ey) { System.out.println(ey.getMessage()); }/////////////////////////////////// sql="select top 10 * from paihangbang order by score desc"; dataBase1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:odbc:xqy", "", "", false, "sun.jdbc.odbc.JdbcOdbcDriver")); queryDataSet1.setQuery(new QueryDescriptor(dataBase1,sql,null,true,Load.ALL)); QuerryList qlist=new QuerryList(); qlist.jdbTable1.setDataSet(queryDataSet1); qlist.setLocation(330,180); qlist.setSize(320,380); qlist.show(); try { conn.close(); sttm.close(); } catch(SQLException er) { System.out.println(er.getMessage()); } }}class QueFrame_jButton1_actionAdapter implements java.awt.event.ActionListener { QueFrame adaptee; QueFrame_jButton1_actionAdapter(QueFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); }}class QueFrame_jButton2_actionAdapter implements java.awt.event.ActionListener { QueFrame adaptee; QueFrame_jButton2_actionAdapter(QueFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed(e); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -