Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.sql.*;
- class Main extends Thread
- {
- Socket s;
- Connection con = null;
- public static void main(String args[])
- {
- String url = "jdbc:oracle:thin:@localhost:1521:orbis";
- Connection con = null;
- try
- {
- if (args.length<2) throw new Exception("Please provide login/password info.");
- String username = args[0];
- String password = args[1];
- System.out.println("Connecting to database..");
- Class.forName("oracle.jdbc.driver.OracleDriver");
- con = DriverManager.getConnection(url, username, password);
- System.out.println("Connection established.");
- int i = 0;
- ServerSocket server = new ServerSocket(1234, 0, InetAddress.getByName("localhost"));
- System.out.println("Accepting incoming connections..");
- while(true)
- {
- new Main(server.accept(), con);
- }
- }
- catch(Exception e)
- {System.out.println("Error: "+e);}
- }
- public Main(Socket s, Connection con)
- {
- this.s = s;
- this.con = con;
- setDaemon(true);
- setPriority(NORM_PRIORITY);
- start();
- }
- public void run()
- {
- try
- {
- InputStream is = s.getInputStream();
- OutputStream os = s.getOutputStream();
- byte buf[] = new byte[64*1024];
- int r = is.read(buf);
- String data = new String(buf, 0, r);
- //con.sendrequest
- System.out.println("Input data: "+data);
- Statement st = null;
- ResultSet res = null;
- st = con.createStatement();
- res = st.executeQuery(data);
- data = "";
- int cnum = res.getMetaData().getColumnCount();
- while(res.next())
- {
- for (int i=0; i<cnum; i++)
- {
- if (i>0) data += " #_# ";
- data += res.getString(i+1);
- }
- data += " #|# ";
- }
- st.close();
- os.write(data.getBytes());
- // завершаем соединение
- s.close();
- }
- catch(Exception e)
- {System.out.println("init error: "+e);} // вывод исключений
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement