News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

INTERACTION BETWEEN SQLSERVER AND SQL CLIENT

Started by pingu.buj, Apr 07, 2008, 02:45 PM

Previous topic - Next topic

pingu.buj

INTERACTION BETWEEN SQLSERVER AND SQL CLIENT

SQLSERVER

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

public class SqlServer
{
  public static void main(String []args)
   {
          BufferedReader br;
         ServerSocket ss;
         Socket s;
         int portno;
         String qry;
         Connection con;
      try
        {
          br=new BufferedReader(new InputStreamReader(System.in));
          System.out.println("enter portno:");
          portno=Integer.parseInt(br.readLine());

          ss=new ServerSocket(portno);
          System.out.println("Server waiting...");

          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

     while(true)
       {
          s=ss.accept();
          new SqlServerReadData(s);
       }
   } catch(Exception e){}
}
}


class SqlServerReadData implements Runnable
{
  Connection con;
Statement stmt;
ResultSet r;
ResultSetMetaData rm;

Socket s;
BufferedReader is;
PrintWriter os;
String qry,result,uname,pass,dsn;
Thread t;
int colcount;

SqlServerReadData(Socket s)
{
try
{
this.s=s;
is=new BufferedReader(new InputStreamReader(s.getInputStream()));
os=new PrintWriter(s.getOutputStream());
//System.out.println("Username");
//uname=is.readLine();
//System.out.println("Passward");
//pass=is.readLine();
//System.out.println("dsn");
dsn=is.readLine();

String str="jdbc:odbc:"+dsn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(str,"","");
stmt=con.createStatement();
t=new Thread(this);
t.start();
}
catch(Exception e){}
}

public void run()
{
try
{
while(true)
{
try
{
qry=is.readLine();
if(qry.compareToIgnoreCase("quit")==0)
break;
if(!(qry.startsWith("select")))
{
try
{
r=stmt.executeQuery(qry);
os.flush();
}catch(Exception e){}
}
else
try
{
r=stmt.executeQuery(qry);
rm=r.getMetaData();
colcount=rm.getColumnCount();

for(int i=1;i<=colcount;i++)
{
result=rm.getColumnLabel(i);
os.print(result);
os.flush();
}
os.println("");
for(int i=1;i<=34;i++)
os.print("-");
os.println("");
while(r.next())
{
result ="";
for(int i=1;i<=colcount;i++)
{
result=result+"       "+r.getString(i);
}

os.println(result);
os.flush();
}
r.close();
}
catch(SQLException e){}
catch(Exception e){}
os.println("end");
os.flush();
}
catch(Exception e){}
}
stmt.close();
}
catch(Exception e){}
}
}


SQLCLIENT

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

public class SqlClient
{
  public static void main(String []args)
   {
    try
      {
          BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));;
          BufferedReader is;
          PrintWriter os;
         Socket s;
         int portno;
         String qry,hostname,result,uname,pass,dsn;

  System.out.print("enter the hostname:");
    hostname=br.readLine();

System.out.println("enter portno:");
portno=Integer.parseInt(br.readLine());

s=new Socket(hostname,portno);
is =new BufferedReader(new InputStreamReader(s.getInputStream()));
os=new PrintWriter(s.getOutputStream());


System.out.println("Username");
uname=is.readLine();
System.out.println("Passward");
pass=is.readLine();
System.out.println("dsn");
dsn=br.readLine();

os.println(uname);
os.flush();

os.println(pass);
os.flush();

os.println(dsn);
os.flush();

while(true)
{
System.out.print("SQL:\>");
qry=br.readLine();
if(qry.equals("quit"))
break;

os.println(qry);
os.flush();

result=is.readLine();
while(!(result.equals("end")))
{
System.out.println(result);
result=is.readLine();
}
}

os.println("quit");
os.flush();
os.close();
is.close();
s.close();
}
catch(Exception e){}
}
? pingu.buj ?