import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.JOptionPane.*;import java.sql.*;
public class MainFrame extends JFrame { //添加菜单 private JMenuBar jMenuBar; private JMenuItem jMIOpen; private JMenu jMenuFile; private JButton jBadd; private JButton jBchange; private JButton jBdelete; private JButton jBbefore; private JButton jBnext; private JLabel jLname; private JTextField jTname; private JLabel jLtelephone; private JTextField jTtelephone; private JLabel jLemail; private JTextField jTemail; private JLabel jLbirthday; private JTextField jTbirthday; private JLabel jLQQ; private JTextField jTQQ; private JPanel jPanel1; private JPanel jPanel2; private JLabel jLimage; private JPanel jPtop; private JLabel jLintroduce; private JTextArea jTintroduce; private JPanel jPbottom; private ResultSet rs; private Statement stmt; private Connection conn;
public MainFrame(){ ///窗体设置 setTitle("通讯纪录本:"); setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); Container contentPane=getContentPane(); //获得屏幕大小 Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screenSize=kit.getScreenSize(); int screenWidth=screenSize.width; int screenHeight=screenSize.height; setLocation(screenWidth/2-DEFAULT_WIDTH/2,screenHeight/2-DEFAULT_HEIGHT/2); /// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //菜单设置 jMenuFile=new JMenu("文件"); jMIOpen=new JMenuItem("打开"); jMenuFile.add(jMIOpen); jMenuBar=new JMenuBar(); jMenuBar.add(jMenuFile); setJMenuBar(jMenuBar);
按钮设置 //添加窗体控件on right jBadd=new JButton("添 加"); jBchange=new JButton("修 改"); jBdelete=new JButton("删 除"); jBbefore=new JButton("上一个"); jBnext=new JButton("下一个"); 添加监听器 jBnext.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ try{ if(rs.next()){ //rs.next(); showResult(); } }catch(SQLException e){ e.printStackTrace(System.err); } } } );
bright=Box.createVerticalBox(); bright.add(jBbefore); bright.add(Box.createVerticalGlue()); bright.add(jBnext); bright.add(Box.createVerticalGlue()); bright.add(jBadd); bright.add(Box.createVerticalGlue()); bright.add(jBchange); bright.add(Box.createVerticalGlue()); bright.add(jBdelete); 取得数据 // jPanel1 jPanel1=new JPanel(new GridLayout(5,2)); jPanel1.setPreferredSize(new Dimension(200,140)); jLname=new JLabel("姓 名:",JLabel.RIGHT); //jLname.setBorder(BorderFactory.createLineBorder(Color.black)); jTname=new JTextField(6); jLtelephone=new JLabel("电 话:",JLabel.RIGHT); jTtelephone=new JTextField(10); jLemail=new JLabel("电子邮件:",JLabel.RIGHT); jTemail=new JTextField(10); jLbirthday=new JLabel("生 日:",JLabel.RIGHT); jTbirthday=new JTextField(12); jLQQ=new JLabel("Q Q:",JLabel.RIGHT); jTQQ=new JTextField(12); / jPanel1.add(jLname); jPanel1.add(jTname); jPanel1.add(jLtelephone); jPanel1.add(jTtelephone); jPanel1.add(jLemail); jPanel1.add(jTemail); jPanel1.add(jLbirthday); jPanel1.add(jTbirthday); jPanel1.add(jLQQ); jPanel1.add(jTQQ); / jPanel2 jPanel2=new JPanel(); jLimage=new JLabel("kkkkkkk"); jLimage.setPreferredSize(new Dimension(200,140)); jLimage.setBorder(BorderFactory.createLineBorder(Color.black)); //jLimage.setBackground(Color.blue); jPanel2.add(jLimage); / jPleft jPleft=new JPanel(new FlowLayout()); // jPtop=new JPanel(new FlowLayout()); jPtop.setBorder(BorderFactory.createLineBorder(Color.blue)); jPtop.setPreferredSize(new Dimension(500,160)); jPtop.add(jPanel1); jPtop.add(Box.createRigidArea(new Dimension(50,100))); jPtop.add(jPanel2); // jPbottom jLintroduce=new JLabel("个人说明:"); jLintroduce.setPreferredSize(new Dimension(460,21)); jTintroduce=new JTextArea("这个娃很懒,什么都没留!!"); jTintroduce.setPreferredSize(new Dimension(460,110)); jPbottom=new JPanel(new FlowLayout()); jPbottom.setBorder(BorderFactory.createLineBorder(Color.black)); jPbottom.setPreferredSize(new Dimension(500,150)); jPbottom.add(jLintroduce); jPbottom.add(jTintroduce); / jPleft.add(jPtop); jPleft.add(jPbottom); / contentPane.add(bright,BorderLayout.WEST); contentPane.add(jPleft,BorderLayout.CENTER); try{ getResult(); showResult(); //closeResult(); }catch(SQLException se){ se.printStackTrace(); } } public void getResult() throws SQLException{ try{ Class.forName(CIDConnect.dbDriver); Connection conn = DriverManager.getConnection(CIDConnect.dbURL,CIDConnect.user,CIDConnect.password); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM information"); if(rs.next()){ showResult(); }; }catch(Exception e){ e.printStackTrace(System.err); } }
public void showResult() throws SQLException{ jTname.setText(rs.getString("dbname")); jTtelephone.setText(rs.getString("dbtele")); jTemail.setText(rs.getString("dbemail")); jTbirthday.setText(rs.getString("dbdate")); jTQQ.setText(rs.getString("dbQQ")); jTintroduce.setText(rs.getString("dbintro")); } public void closeResult() throws SQLException{ conn.close(); stmt.close(); rs.close(); } public static void main(String[] args){ JFrame jFrame=new MainFrame(); jFrame.show(); } private int DEFAULT_WIDTH=600; private int DEFAULT_HEIGHT=400; private Box bright; private JPanel jPcenter; private JPanel jPleft;}
能运行的,但是怎么要抛出no data found呢??????????????