import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class Skin extends JFrame { public static void main(String[] args) { Skin s = new Skin(); s.setVisible(true); // JOptionPane.showMessageDialog(H, "Printing complete"); } private JLabel jLabel; private JLabel jLabel2; private JTextField jTextField; private JTextField jTextField2; private JButton jButton; private JButton jButton2; public Skin() { super(); this.setSize(300, 200); this.getContentPane().setLayout(null); this.add(getJLabel(), null); this.add(getJLabel2(), null); this.add(getJTextField(), null); this.add(getJTextField2(), null); this.add(getJButton(), null); this.add(getJButton2(), null); this.setTitle("Skin"); } private javax.swing.JLabel getJLabel() { if (jLabel == null) { jLabel = new javax.swing.JLabel(); jLabel.setBounds(34, 49, 53, 18); jLabel.setText("公式"); } return jLabel; } private javax.swing.JLabel getJLabel2() { if (jLabel2 == null) { jLabel2 = new javax.swing.JLabel(); jLabel2.setBounds(34, 70, 53, 18); jLabel2.setText("结果"); } return jLabel2; } private javax.swing.JTextField getJTextField() { if (jTextField == null) { jTextField = new javax.swing.JTextField(); jTextField.setBounds(96, 49, 160, 20); } return jTextField; } private javax.swing.JTextField getJTextField2() { if (jTextField2 == null) { jTextField2 = new javax.swing.JTextField(); jTextField2.setBounds(96, 70, 160, 20); } return jTextField2; } private javax.swing.JButton getJButton() { if (jButton == null) { jButton = new javax.swing.JButton(); jButton.setBounds(80, 110, 71, 27); jButton.setText("OK"); jButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { Cal c = new Cal(); jTextField2.setText(c.Calculate(jTextField.getText())); } catch (Exception ee) { // TODO: handle exception jTextField2.setText(ee.toString()); } } }); } return jButton; } private javax.swing.JButton getJButton2() { if (jButton2 == null) { jButton2 = new javax.swing.JButton(); jButton2.setBounds(180, 110, 71, 27); jButton2.setText("QUIT"); jButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { System.exit(0); } catch (Exception ee) { // TODO: handle exception jTextField2.setText(ee.toString()); } } }); } return jButton2; } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.security.auth.login.Configuration; public class Cal { public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String DBURL = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";//需要修改为oracle的数据库链接信息 public static final String DBUSER = "scott";//oracle用户名 public static final String DBPASS = "tiger";//oracle密码 public String Calculate(String s) throws SQLException, ClassNotFoundException { String result = null; Class.forName(DBDRIVER); Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); Statement stmt = conn.createStatement(); String sql = "select " + s + " from dual"; System.out.println(sql); ResultSet rs = stmt.executeQuery(sql); if (rs.next()) { result = rs.getString(1);//这里注意DUA取结果的话必须-->getString(1)而不可以是getString(0). System.out.println(result); } return result; } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.security.auth.login.Configuration; public class Cal { public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String DBURL = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";//需要修改为oracle的数据库链接信息 public static final String DBUSER = "scott";//oracle用户名 public static final String DBPASS = "tiger";//oracle密码 public String Calculate(String s) throws SQLException, ClassNotFoundException { String result = null; Class.forName(DBDRIVER); Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); Statement stmt = conn.createStatement(); String sql = "select " + s + " from dual"; System.out.println(sql); ResultSet rs = stmt.executeQuery(sql); if (rs.next()) { result = rs.getString(1);//这里注意DUA取结果的话必须-->getString(1)而不可以是getString(0). System.out.println(result); } return result; } }
这样实现的优点是可以把各种复杂的运算特殊情况交给oracle来处理,节省了代码,也减轻了工作量。
缺点当然很明显了。。。得配置oracle信息。
界面效果: