3道编程题!~~各位高手来帮帮我啊!小女子先谢谢了!~ 1.写一个Java程序实现线程连接池功能2.写一个服务器端程序实现在客户端输入字符然后在控制台显示,直到输入“END”时停止3.用JavaSocket编程,读服务器几个字符,再写入本地显示一题20分! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我这正好有一个,LZ参考!import java.sql.*;import java.util.*;public class ConnectionPool { private static Vector freeConnetions = new Vector(); private static Vector usingConnections = new Vector(); //获取一个空闲连接 public static Connection getConnection() { Connection connection = null; if (freeConnetions.size() > 0) { connection = (Connection) freeConnetions.remove(0); } else { try { Class.forName("org.gjt.mm.mysql.Driver"); connection = DriverManager.getConnection ("jdbc:mysql://127.0.0.1/zl?characterEncoding=gb18030"); usingConnections.add(connection); } catch (ClassNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (SQLException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } finally { } } return connection; } //释放一个连接 public static void freeConnection(Connection con) { usingConnections.remove(con); freeConnetions.add(con); }} 2题:(不知道对不对哈,大家别笑我哈)package test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;//实现从客户端输入的方法class Input{ String str = null; public void input_method(){ System.out.println("请输入字符串(输入end后结束):"); BufferedReader sb = new BufferedReader(new InputStreamReader(System.in)); try{ //接受从键盘输入的字符串 str = sb.readLine(); }catch(IOException e){ System.out.println("IOException"); } //判断是否输入的是"end"字符串 if(str.equalsIgnoreCase("end")){ System.exit(1); } //在控制台打印你输入的字符串 else{ System.out.println("------"+str+"------"); Input ips = new Input(); ips.input_method(); } }}public class Demo { public static void main(String[] args) { Input ips = new Input(); //调用Input类中的方法 ips.input_method(); }} 楼主学JAVA不要死记硬背啊,唉~ 怎么最近服务器客户端socket程序这么吃香.lz过来看看这个程序: http://blog.sina.com.cn/u/550626e7010008bq改改就能符合你的要求了.如果你改都不会,那就不要到这里发帖子了.程序说明: 服务器端使用的是ServerSocket,通过创建ServerSocket来绑定服务器端口,服务器端创建一个进程监听这个端口,当有客户端连接后,则接收客户端发送的消息,并打印在控制台。客户端则是通过连接服务器的指定端口来获得socket,并通过socket写入信息,每次可以输入一行字符,如果输入了exit,则退出程序。 第二个刚好有个现成的//MyServer.javaimport java.io.*;import java.net.*;public class MyServer{ public static void main(String[] args)throws Exception{ ServerSocket server=new ServerSocket(1352); Socket client = server.accept(); BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out=new PrintWriter(client.getOutputStream()); while(true){ String str=in.readLine(); out.println(str); out.print("has received:"); out.flush(); if(str.equals("929")){ break; } } client.close(); } }//Client.javaimport java.io.*;import java.net.*;public class Client{ static Socket server; public static void main(String[] args)throws Exception{ server=new Socket(InetAddress.getLocalHost(),1352); BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream())); PrintWriter out=new PrintWriter(server.getOutputStream()); BufferedReader wt=new BufferedReader(new InputStreamReader(System.in)); while(true){ String str=wt.readLine(); out.println(str); out.flush(); if(str.equals("29")){ break; } System.out.println(in.readLine()); } server.close(); } } 物理现象模拟 java如何打开文本文件 jtree的问题 myEclipse6.0的问题 关于环境变量 【求助】高人指点:如何使用TimeSeries 【JFreeChart1.0.5版本中】 请教关于classloader的问题。 为什么我在import证书时出现如下错误 通过main(String argv[])的argv[]接收一个数字字符,怎样转换成int啊?谢谢, applet访问本地硬盘,怎末实现 请教简单的数组问题 急:为何这个程序没有用到paint方法,但又实现了他的功能!!!!
import java.sql.*;
import java.util.*;public class ConnectionPool {
private static Vector freeConnetions = new Vector();
private static Vector usingConnections = new Vector(); //获取一个空闲连接
public static Connection getConnection() {
Connection connection = null;
if (freeConnetions.size() > 0) {
connection = (Connection) freeConnetions.remove(0);
} else {
try {
Class.forName("org.gjt.mm.mysql.Driver");
connection = DriverManager.getConnection
("jdbc:mysql://127.0.0.1/zl?characterEncoding=gb18030");
usingConnections.add(connection);
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} finally {
}
}
return connection;
} //释放一个连接
public static void freeConnection(Connection con) {
usingConnections.remove(con);
freeConnetions.add(con); }
}
package test;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;//实现从客户端输入的方法
class Input{
String str = null;
public void input_method(){
System.out.println("请输入字符串(输入end后结束):");
BufferedReader sb = new BufferedReader(new InputStreamReader(System.in));
try{
//接受从键盘输入的字符串
str = sb.readLine();
}catch(IOException e){
System.out.println("IOException");
}
//判断是否输入的是"end"字符串
if(str.equalsIgnoreCase("end")){
System.exit(1);
}
//在控制台打印你输入的字符串
else{
System.out.println("------"+str+"------");
Input ips = new Input();
ips.input_method();
}
}
}
public class Demo { public static void main(String[] args) {
Input ips = new Input();
//调用Input类中的方法
ips.input_method();
}
}
lz过来看看这个程序:
http://blog.sina.com.cn/u/550626e7010008bq
改改就能符合你的要求了.如果你改都不会,那就不要到这里发帖子了.
程序说明:
服务器端使用的是ServerSocket,通过创建ServerSocket来绑定服务器端口,服务器端创建一个进程监听这个端口,当有客户端连接后,则接收客户端发送的消息,并打印在控制台。客户端则是通过连接服务器的指定端口来获得socket,并通过socket写入信息,每次可以输入一行字符,如果输入了exit,则退出程序。
//MyServer.java
import java.io.*;
import java.net.*;
public class MyServer{
public static void main(String[] args)throws Exception{
ServerSocket server=new ServerSocket(1352);
Socket client = server.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
while(true){
String str=in.readLine();
out.println(str);
out.print("has received:");
out.flush();
if(str.equals("929")){
break;
}
}
client.close();
}
}//Client.java
import java.io.*;
import java.net.*;
public class Client{
static Socket server;
public static void main(String[] args)throws Exception{
server=new Socket(InetAddress.getLocalHost(),1352);
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter out=new PrintWriter(server.getOutputStream());
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
while(true){
String str=wt.readLine();
out.println(str);
out.flush();
if(str.equals("29")){
break;
}
System.out.println(in.readLine());
}
server.close();
}
}