发表于: 2018-02-11 22:35:41

1 682


今日完成

1.学习网络连接;

   TCP:TCP 是传输控制协议的缩写,它保障了两个应用程序之间的可靠通信。通常用于互联网协议,被称 TCP / IP。

   UDP:UDP 是用户数据报协议的缩写,一个无连接的协议。提供了应用程序之间要发送的数据的数据包。

   套接字(Socket):用于将应用程序与端口连接起来。就像“插座”用于连接电器和电线一样,插座就是套接字;

  客户端程序创建一个套接字,并尝试连接服务器的套接字。

  当连接建立时,服务器会创建一个 Socket 对象。客户端和服务器现在可以通过对 Socket 对象的写入和读取来进行通信。

 InetAddress类,利用该类可以获取IP地址、主机地址

public class Adderss {
public static void main(String[] args) {
InetAddress ip;
       try {
ip=InetAddress.getLocalHost();
           String localhostname=ip.getHostName();
           String localhostaddress=ip.getHostAddress();
           System.out.println(localhostname+":"+localhostaddress);
       } catch (UnknownHostException e) {
e.printStackTrace();
       }
}

ServerSocket类:其主要功能是等待来自网络上的“请求”

public class SevrtSocketTest {
private BufferedReader reader;
  private ServerSocket server;
  private Socket socket;
  void getserver(){
try {
server=new ServerSocket(8998);
          System.out.println("服务器套接字创建成功");
          while (true) {
System.out.println("等待客户将连接");
              socket = server.accept();
              reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              getClientMessage();
          }
} catch (Exception e) {
e.printStackTrace();
      }

}
private void getClientMessage() {
try {
while (true) {
System.out.println("客户机" + reader.readLine());
             }
} catch(IOException e){
e.printStackTrace();
          }
try {
if (reader != null) {
reader.close();
              }
if (socket != null) {
socket.close();
              }
} catch (IOException e) {
e.printStackTrace();
          }

}
public static void main(String[] args) {
SevrtSocketTest tcp=new SevrtSocketTest();
       tcp.getserver();
   }
}

 

2.完善任务,重写JDBCTemplate;

  在师兄的指导下,将JDBCTemplate代码完善;

测试方法:

public class Test {
public static void main(String[] args) {
Select q=new Select();
       q.selectByID(1,3);
   }
}


public class Select {
   Connection con;
   String a;
   PreparedStatement sql;
   String stb;
   ResultSet res;

   public void selectByID(int a,int b){
      TemplateDemo c=new TemplateDemo();
         con=c.open();
         try {
           stb="select * from student_copy where ID=?";
           sql=con.prepareStatement(stb);
           sql.setInt(a,b);
           res=sql.executeQuery();
           while (res.next()) {
               Long ID = res.getLong("ID");
               String name = res.getString("name");
               Long QQ = res.getLong("QQ");
               String onlineID = res.getString("onlineID");
               String time_of_enrollment = res.getString("time_of_enrollment");
               String graduate_institutions = res.getString("graduate_institutions");
               String report_link = res.getString("report_link");
               String swear = res.getString("swear");
               String hearfrom = res.getString("hearfrom");
               System.out.print(ID);
               System.out.print(name);
               System.out.print(QQ);
               System.out.print(onlineID);
               System.out.print(time_of_enrollment);
               System.out.print(graduate_institutions);
               System.out.print(report_link);
               System.out.print(ID);
               System.out.println(hearfrom);
              }

           } catch (SQLException e) {
e.printStackTrace();
       }
}
}


public class GetProperties {
Reader r;
       public Properties GetProp(){
Properties prop=new Properties();
           try {

r=new FileReader("E:\\Mypractice\\src\\main\\resources\\db.properties");
           } catch (FileNotFoundException e) {
e.printStackTrace();
           }
try {

prop.load(r);
           } catch (IOException e) {
e.printStackTrace();
           }
return prop;
       }


driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/wodeshujuku
name=root
password=w7217459

明天计划

1.将増改删的代码加入上面JDBCTempla代码;

遇到问题

------

收获

学习了部分网络编程


返回列表 返回列表
评论

    分享到