若干个经典的JAVA小程序源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
pre.close(); conn.close(); //重定向至查询页面 out.println("成功保存!!"); response.sendRedirect("emp.html");
} catch (SQLException ss) {
ss.printStackTrace(); response.sendRedirect("/WebModule1/error.html");
}
}
//ServerThrБайду номын сангаасad.java 代码 package message; import java.net.*; import java.io.*; import java.util.*; public class ServerThread extends Thread{
DataInputStream in;
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.sql.SQLException; import java.sql.*;
public class emp extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK";
class cloneFrame extends JFrame { public cloneFrame() {
setTitle("机试程序"); setSize(600, 400);
JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(new Color(56,43,85));
20);
field1 = new JTextField("姓名:老孙;学号:2004132028",
field2 = new JTextField("反转", 20); field2.setEditable(false); textPanel.add(field1);
textPanel.add(field2);
String pay = request.getParameter("pay"); System.out.println("empcode:" + code); System.out.println("name:" + name); System.out.println("pay:" + pay); //创建驱动 new com.microsoft.jdbc.sqlserver.SQLServerDriver(); String strd = "jdbc:microsoft:sqlserver://localhost:1433;databasename=emp_dates"; String username = "sa"; String pws = ""; try { java.sql.Connection conn = java.sql.DriverManager.getConnection( strd, username, pws); String strs = "insert into emp values(?,?,?)"; java.sql.PreparedStatement pre = conn.prepareStatement(strs); pre.setString(1, code); pre.setString(2, name); pre.setString(3, pay); pre.execute();
}
private class turnListener implements ActionListener
{
public void actionPerformed(ActionEvent event) {
String gets = field1.getText();
int i = gets.length(); StringBuffer buffer = new StringBuffer(i); for(int j=i-1;j>=0;j--) { buffer.append(gets.charAt(j)); }
Java2-聊天程序
//这是个聊天程序, 在 ECLIPSE 运行 Client.java 就可以了。 连接是: localhost //Server 代码,
package message; import java.io.*; import java.net.*; import java.util.*; public class Server {
一个 Java 小程序源代码(反转字符串)
//BorderLayoutTest.java
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*;
{ buffer.append(gets.charAt(j));
}
String gets2 = buffer.toString(); field2.setText(gets2);
} }
Container contentPane = getContentPane(); contentPane.add(buttonPanel, BorderLayout.SOUTH); contentPane.add(textPanel, BorderLayout.NORTH); contentPane.add(tuPanel, BorderLayout.CENTER); contentPane.setBackground(new Color(100,100,100));
String gets2 = buffer.toString(); field2.setText(gets2);
} }
private JButton Button1; private JTextField field1; private JTextField field2; }
Java1
package com.kenki.emp;
Vector all; public ServerThread(DataInputStream in,Vector v){ this.in=in; this.all=v;
}
public void run() { while(true) { try{ String s1=in.readUTF(); for(int i=0;i<all.size();i++) { Object obj=all.get(i); Socket socket=(Socket)obj; DataOutputStream out=new DataOutputStream(socket.getOutputStream()); out.writeUTF(s1); System.out.print(i); out.flush();
Button1 = new JButton("点击复制反转字符串"); Button1.addActionListener(new turnListener()); buttonPanel.add(Button1);
JPanel textPanel = new JPanel(); textPanel.setBackground(new Color(100,100,100));
public static void main(String[] args) throws Exception{ System.out.print("Server"); ServerSocket socket=new ServerSocket(8888); Vector v=new Vector(); while(true){ Socket sk=socket.accept(); DataInputStream in=new DataInputStream(sk.getInputStream()); DataOutputStream out=new DataOutputStream(sk.getOutputStream()); v.add(sk); new ServerThread(in,v).start(); }
Container contentPane = getContentPane(); contentPane.add(buttonPanel, BorderLayout.SOUTH); contentPane.add(textPanel, BorderLayout.NORTH); contentPane.add(tuPanel, BorderLayout.CENTER); contentPane.setBackground(new Color(100,100,100)); }
private class turnListener implements ActionListener
{ public void actionPerformed(ActionEvent event) { String gets = field1.getText();
int i = gets.length(); StringBuffer buffer = new StringBuffer(i); for(int j=i-1;j>=0;j--)
//Initialize global variables public void init() throws ServletException { }
//Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); String code = request.getParameter("code"); String name = request.getParameter("name");
JPanel tuPanel = new JPanel(); JLabel label = new JLabel(new ImageIcon("006.gif")); tuPanel.add(label); tuPanel.setBackground(new Color(100,100,100));
} } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } //Clean up resources public void destroy() { } }
} System.out.print("Message send over!"); }catch(Exception e){e.printStackTrace();};
public class test1 { public static void main(String[] args) {
cloneFrame frame = new cloneFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } }
相关文档
最新文档