networking authentication program 1.0
Submitted By:
manoveg
Rating:
(Not rated) (
Rate It)
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
public class Server
{
Connection conn=null;
public Server() throws SQLException , IOException
{
Properties props=new Properties();
FileInputStream in=new FileInputStream("D:/java/java/bin/manoveg/server/database.properties");
props.load(in);
in.close();
String drivers=props.getProperty("jdbc.drivers");
if(drivers!=null)
System.setProperty("jdbc.drivers",drivers);
String url=props.getProperty("jdbc.url");
String username=props.getProperty("jdbc.username");
String password=props.getProperty("jdbc.password");
conn=DriverManager.getConnection(url,username,password);
}
public static void main(String args[]) throws Exception
{
Server obj=new Server();
try
{
ServerSocket s = new ServerSocket(8189);
for(;;)
{
Socket incoming = s.accept();
Thread t = new ThreadHandler(incoming,obj.conn);
t.start();
if(t.isAlive())
{
t.join();
}
System.out.println(" main end");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class ThreadHandler extends Thread
{
private Socket incoming;
private int counter;
Connection conn;
String str="";
PrintWriter out;
public ThreadHandler(Socket i,Connection conn1)
{
incoming=i;
conn=conn1;
}
public void getData()
{
try
{
// BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
InputStream ism=incoming.getInputStream();
System.out.println("Server Running Enter Exit to quit.");
boolean done= false;
int ch=0,i=0;
char[] str1=new char[20];
while((ch=ism.read())!=-1)
{
str1[i]=(char)ch;
i++;
}
str=new String(str1);
if(str==null || str.equals(""))
{
System.out.println("str==null");
done=true;
}
else if(str.trim().equals("Exit"))
done=true;
else
{
System.out.println("Echo:"+str);
this.test(str);
System.out.println("test");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
void test(String str3) throws Exception
{
boolean flag=false;
out = new PrintWriter(incoming.getOutputStream(),true);
String sqls="Select * from mylogin";
Statement stat=conn.createStatement();
ResultSet rs=stat.executeQuery(sqls);
System.out.println("resultset");
while(rs.next())
{
if(rs.getString("username").equals(str3))
{
flag=true;
break;
}
else
{
flag=false;
}
}
if(flag==true)
{
out.print("Login Successful");
System.out.println("Found User "+str3);
}
else
System.out.println("User Not Found "+str3);
rs.close();
}
public void run()
{
synchronized(this)
{
this.getData();
}
}
}