Socket通信
// 服务器端
import java.io.*;
import java.net.*;
import java.util.*;
public class MyChatServer
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
ServerSocket server = null;
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
try{
server = new ServerSocket(2345);
socket = server.accept();
out = new PrintWriter(socket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// out.println("xxx");
// sin = in.readLine();
// scan.next();
} catch (IOException e) {
e.printStackTrace();
} finally {
//此处为严谨写法,也可根据情况进行修改
if(null!=out){
out.close();
}
try {
if( null!=in ){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if( null!=socket ){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if( null!=server ){
server.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 客户端
import java.io.*;
import java.net.*;
import java.util.*;
public class MyChatClient
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
try{
socket = new Socket("127.0.0.1",2345);
out = new PrintWriter(socket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// out.println("xxx");
// sin = in.readLine();
// scan.next();
} catch (IOException e)
{
System.out.println("Error"+e);
} finally {
if(out!=null){
out.close();
}
try {
if( null!=in ){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if( null!=socket ){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
数据库操作
// 数据库
import java.sql.*;
public class SQL {
public static void main(String[] args) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL =
"jdbc:sqlserver://localhost:1433;DatabaseName=xxx;user=xxx;password=xxx";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName(driverName);
connection = DriverManager.getConnection(dbURL);
statement = connection.createStatement();
String sql = "sql语句";
resultSet = statement.executeQuery(sql);
while(resultSet.next()) {
String value1 = resultSet.getString("列名");
String value2 = resultSet.getString(1); // 从1开始
}
// String s = "insert into student(Name,Sex,Age) values('张飒','女',20)";
// statement.executeUpdate(s);
// ResultSetMetaData rsmd = resultSet.getMetaData();
// int columns = rsmd.getColumnCount();
// String columnName = rsmd.getColumnLabel(1);
} catch (ClassNotFoundException e) {
// Class.forName()
e.printStackTrace();
} catch (SQLException e) {
// DriverManager.getConnection()
e.printStackTrace();
} finally {
try {
if(null!=resultSet) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(null!=statement) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(null!=connection) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
祝大家取得好成绩!