2

我写了一个java服务器套接字,它可以连接postgresql并返回值,但是当我编译javac -deprecation SQLServer.java时为什么警告不起作用?它没有任何错误。我该如何解决?

SQLServer.java:66: warning: [deprecation] readLine() in DataInputStream has been     deprecated
                            query = in.readLine();
                                      ^
1 warning

代码:

import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;

public class SQLServer extends Thread{
    public static final int MYPORT = 6700;
    ServerSocket listen_socket;

        public SQLServer(){
        try{
            listen_socket = new ServerSocket (MYPORT);
        }
        catch(IOException e) {System.err.println(e);}
        this.start();
    }
    public void run(){
        try{
            while(true)
            {
                Socket client_socket = listen_socket.accept();
                SQLConnection c = new SQLConnection (client_socket);
            }
        }
        catch(IOException e) {System.err.println(e);}
    }
    public static void main(String[] argv){
       new SQLServer();
    }
}
class SQLConnection extends Thread{
    protected Socket client;
    protected DataInputStream in;
    protected PrintStream out;
    protected String query;

    public SQLConnection (Socket client_socket){
        client = client_socket;
        try{
            in = new DataInputStream(client.getInputStream());
            out = new PrintStream (client.getOutputStream());
        }
        catch(IOException e) {
            System.err.println(e);
            try {client.close();} 
            catch (IOException e2) {};
            return;
        }
        this.start();

    }

    public void run(){
        try{
            query = in.readLine();
            System.out.println("read query string <" + query + ">");
            do_sql();
            //out.println(d.toString());
        }
        catch (IOException e) {}
        finally{
            try {client.close();}
            catch (IOException e) {};
        }
    }
        public void do_sql(){
        Connection con; // database connection object
        Statement stmt; // SQL statement object 
        ResultSet rs;   // SQL query results
        ResultSetMetaData rsmd;
        boolean more;   // "more rows found" switch
        String dsn = "jdbc:postgresql://localhost/postgres"; 
        String user = "postgres"; 
        String password = "123456";
        String record;
        int colcount, i;

        try{
            Class.forName ("org.postgresql.Driver");
            con = DriverManager.getConnection(dsn, user, password);

            stmt = con.createStatement();

            rs = stmt.executeQuery(query);

            more = rs.next(); 
            if (!more) {
                out.println("No rows found."); 
                return;
            }
            rsmd = rs.getMetaData();
            colcount = rsmd.getColumnCount();
            System.out.println(colcount + " columns in result set");

            while (more) {
                //build result string
                record = "";
                for (i=1; i <= colcount; i++){
                   record = record.concat(rs.getString(i) + "  ");
                }
            out.println(record);
            System.out.println(record);
            more = rs.next();
        }
        rs.close(); 
        stmt.close();
        con.commit(); 
        con.close();
        }
        catch (Exception e)
        {
          System.out.println("Exception occurred" + e.toString());
        }
       }
}
4

3 回答 3

12

来自DataInputStream#readLinejavadoc:

已弃用。此方法不能正确地将字节转换为字符。从 JDK 1.1 开始,读取文本行的首选方法是通过BufferedReader.readLine()方法。使用 DataInputStream 类读取行的程序可以通过替换形式的代码转换为使用 BufferedReader 类:

    DataInputStream d = new DataInputStream(in);

和:

    BufferedReader d = new BufferedReader(new InputStreamReader(in));
于 2013-09-23T14:22:14.090 回答
0

该方法已被弃用,因为 Streams 旨在处理字节,以处理我们长期以来拥有的文本 Readers & Writers 因为它们提供了使用编码的功能。

如果您想阅读文本,请尝试将您的流包装到阅读器中

BufferedReader r = new BufferedReader(new InputStreamreader(your data stream, optional encoding parameter like "UTF-8"))

r.readLine();
于 2013-09-23T14:23:52.727 回答
0

这就是 Java API 所说的:

已弃用。此方法不能正确地将字节转换为字符。从 JDK 1.1 开始,读取文本行的首选方法是通过 BufferedReader.readLine() 方法。使用 DataInputStream 类读取行的程序可以通过替换形式的代码转换为使用 BufferedReader 类:

     DataInputStream d = new DataInputStream(in);

和:

     BufferedReader d
          = new BufferedReader(new InputStreamReader(in));
于 2013-09-23T14:24:47.290 回答