您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > java通讯录代码全
packagecom.tel;importjava.awt.*;importjavax.swing.*;//创建面板publicclassFrame1extendsJFrame{JPanelcontentPane;BorderLayoutborderLayout1=newBorderLayout();publicFrame1(){try{setDefaultCloseOperation(EXIT_ON_CLOSE);jbInit();}catch(Exceptionexception){exception.printStackTrace();}}/***Componentinitialization.**@throwsjava.lang.Exception*/privatevoidjbInit()throwsException{contentPane=(JPanel)getContentPane();contentPane.setLayout(borderLayout1);setSize(newDimension(400,300));setTitle(FrameTitle);}}packagecom.tel;importjava.awt.*;importjava.awt.event.*;classMyWindowListenerextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){System.exit(1);}}packagecom.tel;//添加功能代码importjava.awt.*;importjava.awt.event.*;importjava.sql.*;importjavax.swing.*;publicclassTelAddimplementsActionListener{privateTelFieldsfields;privateJTextAreata;privateConnectioncon;publicTelAdd(Connectionc,TelFieldsf,JTextAreaa){con=c;fields=f;ta=a;}publicvoidactionPerformed(ActionEvente){try{//执行insert语句添加新数据PreparedStatementpstmt=con.prepareStatement(insertintotongxunvalues(?,?));Stringname=fields.name.getText();Stringtel=fields.tel.getText();pstmt.setString(1,name);pstmt.setString(2,tel);pstmt.executeUpdate();pstmt.close();ta.setText(SQL语句:\nINSERTINTOtongxunVALUES('+name+','+tel+')+\n加入成功);}catch(SQLExceptionex){System.err.println(SQLException:+ex.getMessage());}}publicvoidshowResultSet(ResultSetrs){try{if(rs.next())fields.tel.setText(rs.getString(2));elsefields.tel.setText(找不到合乎条件的记录);}catch(SQLExceptione3){e3.printStackTrace();ta.append(\n+e3.toString()+\n);}}}packagecom.tel;//设计按钮importjava.awt.*;importjava.awt.event.*;importjava.sql.*;importjavax.swing.*;publicclassTelButtonsextendsJPanel{publicTelButtons(){try{jbInit();}catch(Exceptionex){ex.printStackTrace();}}privateJButtonfindButton,addButton,updateButton;privateJButtonsqlButton,deleteButton,helpButton;publicTelButtons(Connectionc,TelFieldsf,JTextAreata){setLayout(newGridLayout(1,6));findButton=newJButton(查找);findButton.addActionListener(newTelFind(c,f,ta));add(findButton);addButton=newJButton(添加);addButton.addActionListener(newTelAdd(c,f,ta));add(addButton);updateButton=newJButton(更新);updateButton.addActionListener(newTelUpdate(c,f,ta));add(updateButton);deleteButton=newJButton(删除);deleteButton.addActionListener(newTelDelete(c,f,ta));add(deleteButton);helpButton=newJButton(说明);helpButton.addActionListener(newTelHelp(ta));add(helpButton);}privatevoidjbInit()throwsException{}}packagecom.tel;//删除功能代码importjava.awt.*;importjava.awt.event.*;importjava.sql.*;importjavax.swing.*;publicclassTelDeleteimplementsActionListener{privateTelFieldsfields;privateJTextAreata;privateConnectioncon;publicTelDelete(Connectionc,TelFieldsf,JTextAreaa){con=c;fields=f;ta=a;}publicvoidactionPerformed(ActionEvente){try{Statementstmt=con.createStatement();if(!fields.tel.getText().equals()){//用delete语句删除数据Stringquery=deletefromtongxunwheretel='+fields.tel.getText()+';ta.setText(SQL查询:\n+query+\n);intresult=stmt.executeUpdate(query);if(result==1)ta.append(删除成功\n);else{ta.append(删除失败\n);fields.name.setText();fields.tel.setText();}stmt.close();}else{ta.setText(您只可删除已有记录\n);}}catch(SQLExceptione2){e2.printStackTrace();ta.append(\n+e2.toString()+\n);}}}packagecom.tel;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassTelFieldsextendsJPanel{privateJPanellabelPanel,fieldPanel;privateString[]labels={姓名:,电话:};JTextFieldname,tel;publicTelFields(){labelPanel=newJPanel();labelPanel.setLayout(newGridLayout(labels.length,1));for(inti=0;ilabels.length;i++)labelPanel.add(newJLabel(labels[i],0));fieldPanel=newJPanel();fieldPanel.setLayout(newGridLayout(labels.length,1));name=newJTextField(10);fieldPanel.add(name);tel=newJTextField(12);fieldPanel.add(tel);setLayout(newGridLayout(1,2));add(labelPanel);add(fieldPanel);}}packagecom.tel;//查询功能代码importjava.awt.*;importjava.awt.event.*;importjava.sql.*;importjavax.swing.*;publicclassTelFindimplementsActionListener{privateTelFieldsfields;privateJTextAreata;privateConnectioncon;publicTelFind(Connectionc,TelFieldsf,JTextAreaa){con=c;fields=f;ta=a;}publicvoidactionPerformed(ActionEvente){try{if(!fields.name.getText().equals()){Statementstmt=con.createStatement();//用select语句查询,关键字为name中的信息Stringquery=SELECT*FROMtongxunWHERENameLIKE'%+fields.name.getText()+%';ta.setText(SQL查询:\n+query+\n);ResultSetrs=stmt.executeQuery(query);showResultSet(rs);ta.append(查询成功\n);stmt.close();}else{ta.setText(请输入姓名后按查找按钮\n);}}catch(SQLExceptione2){e2.printStackTrace();ta.append(\n+e2.toString()+\n);}}publicvoidshowResultSet(ResultSetrs){try{if(rs.next()){ta.append(rs.getString(1)+\t+rs.getString(2)+\n);while(rs.next())ta.append(rs.getString(1)+\t+rs.getString(2)+\n);}elsefields.tel.setText(找不到合乎条件的记录);}catch(SQLExceptione3){e3.printStackTrace();ta.append(\n+e3.toString()+\n);}}}packagecom.tel;importjava.awt.*;importjava.awt.event.*;importjava.sql.*;importjavax.swing.*;publicclassTelHelpimplementsActionListener{privateJTextAreata;publicTelHelp(JTextAreaa){ta=a;}publicvoidactionPerformed(Action
本文标题:java通讯录代码全
链接地址:https://www.777doc.com/doc-4391537 .html