Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. import java.sql.*;
  5.  
  6. class Main extends Thread
  7. {
  8.     Socket s;
  9.     Connection con = null;
  10.  
  11.     public static void main(String args[])
  12.     {
  13.        
  14.         String url = "jdbc:oracle:thin:@localhost:1521:orbis";
  15.         Connection con = null;
  16.        
  17.         try
  18.         {
  19.             if (args.length<2) throw new Exception("Please provide login/password info.");
  20.             String username = args[0];
  21.             String password = args[1];
  22.             System.out.println("Connecting to database..");
  23.             Class.forName("oracle.jdbc.driver.OracleDriver");
  24.             con = DriverManager.getConnection(url, username, password);
  25.             System.out.println("Connection established.");
  26.             int i = 0;
  27.  
  28.             ServerSocket server = new ServerSocket(1234, 0, InetAddress.getByName("localhost"));
  29.  
  30.             System.out.println("Accepting incoming connections..");
  31.  
  32.             while(true)
  33.             {
  34.                 new Main(server.accept(), con);
  35.             }
  36.         }
  37.         catch(Exception e)
  38.         {System.out.println("Error: "+e);}
  39.     }
  40.  
  41.     public Main(Socket s, Connection con)
  42.     {
  43.         this.s = s;
  44.         this.con = con;
  45.         setDaemon(true);
  46.         setPriority(NORM_PRIORITY);
  47.         start();
  48.     }
  49.  
  50.     public void run()
  51.     {
  52.         try
  53.         {
  54.             InputStream is = s.getInputStream();
  55.             OutputStream os = s.getOutputStream();
  56.  
  57.             byte buf[] = new byte[64*1024];
  58.             int r = is.read(buf);
  59.             String data = new String(buf, 0, r);
  60.             //con.sendrequest
  61.             System.out.println("Input data:  "+data);
  62.            
  63.             Statement st = null;
  64.             ResultSet res = null;
  65.             st = con.createStatement();
  66.             res = st.executeQuery(data);
  67.             data = "";
  68.             int cnum = res.getMetaData().getColumnCount();
  69.             while(res.next())
  70.             {
  71.                 for (int i=0; i<cnum; i++)
  72.                 {
  73.                     if (i>0) data += " #_# ";
  74.                     data += res.getString(i+1);                
  75.                 }
  76.                 data += " #|# ";
  77.             }
  78.            
  79.             st.close();
  80.            
  81.             os.write(data.getBytes());
  82.  
  83.             // завершаем соединение
  84.             s.close();
  85.         }
  86.         catch(Exception e)
  87.         {System.out.println("init error: "+e);} // вывод исключений
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement