若干个经典Java小程序源代码Word文件下载.docx
《若干个经典Java小程序源代码Word文件下载.docx》由会员分享,可在线阅读,更多相关《若干个经典Java小程序源代码Word文件下载.docx(31页珍藏版)》请在冰豆网上搜索。
}
classcloneFrameextendsJFrame
{
publiccloneFrame()
setTitle("
机试程序"
);
setSize(600,400);
JPanelbuttonPanel=newJPanel();
buttonPanel.setBackground(newColor(56,43,85));
Button1=newJButton("
点击复制反转字符串"
Button1.addActionListener(newturnListener());
buttonPanel.add(Button1);
JPaneltextPanel=newJPanel();
textPanel.setBackground(newColor(100,100,100));
field1=newJTextField("
姓名:
老孙;
学号:
**********"
20);
field2=newJTextField("
反转"
field2.setEditable(false);
textPanel.add(field1);
textPanel.add(field2);
JPaneltuPanel=newJPanel();
JLabellabel=newJLabel(newImageIcon("
006.gif"
));
tuPanel.add(label);
tuPanel.setBackground(newColor(100,100,100));
ContainercontentPane=getContentPane();
contentPane.add(buttonPanel,BorderLayout.SOUTH);
contentPane.add(textPanel,BorderLayout.NORTH);
contentPane.add(tuPanel,BorderLayout.CENTER);
contentPane.setBackground(newColor(100,100,100));
privateclassturnListener
implementsActionListener
publicvoidactionPerformed(ActionEventevent)
{
Stringgets=field1.getText();
inti=gets.length();
StringBufferbuffer=newStringBuffer(i);
for(intj=i-1;
j>
=0;
j--)
buffer.append(gets.charAt(j));
}
Stringgets2=buffer.toString();
field2.setText(gets2);
privateJButtonButton1;
privateJTextFieldfield1;
privateJTextFieldfield2;
Java1
packagecom.kenki.emp;
importjavax.servlet.*;
importjavax.servlet.http.*;
importjava.io.*;
importjava.util.*;
importjava.sql.SQLException;
importjava.sql.*;
publicclassempextendsHttpServlet{
privatestaticfinalStringCONTENT_TYPE="
text/html;
charset=GBK"
;
//Initializeglobalvariables
publicvoidinit()throwsServletException{
}
//ProcesstheHTTPGetrequest
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throws
ServletException,IOException{
response.setContentType(CONTENT_TYPE);
PrintWriterout=response.getWriter();
Stringcode=request.getParameter("
code"
Stringname=request.getParameter("
name"
Stringpay=request.getParameter("
pay"
System.out.println("
empcode:
"
+code);
name:
+name);
pay:
+pay);
//创建驱动
newcom.microsoft.jdbc.sqlserver.SQLServerDriver();
Stringstrd=
jdbc:
microsoft:
sqlserver:
//localhost:
1433;
databasename=emp_dates"
Stringusername="
sa"
Stringpws="
try{
java.sql.Connectionconn=java.sql.DriverManager.getConnection(
strd,username,pws);
Stringstrs="
insertintoempvalues(?
?
)"
java.sql.PreparedStatementpre=conn.prepareStatement(strs);
pre.setString(1,code);
pre.setString(2,name);
pre.setString(3,pay);
pre.execute();
pre.close();
conn.close();
//重定向至查询页面
out.println("
成功保存!
!
response.sendRedirect("
emp.html"
}catch(SQLExceptionss){
ss.printStackTrace();
/WebModule1/error.html"
//ProcesstheHTTPPostrequest
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throws
doGet(request,response);
//Cleanupresources
publicvoiddestroy(){
Java2-聊天程序
//这是个聊天程序,在ECLIPSE运行Client.java就可以了。
连接是:
localhost
//Server代码,
packagemessage;
import.*;
publicclassServer{
publicstaticvoidmain(String[]args)throwsException{
System.out.print("
Server"
ServerSocketsocket=newServerSocket(8888);
Vectorv=newVector();
while(true){
Socketsk=socket.accept();
DataInputStreamin=newDataInp