// Program : GPRS Packet fetching and saved it into Database // Author : Himachandra C import java.io.*; import java.net.*; import java.sql.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String Str1 = "$S"; String Str2; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db = "testserver"; String driver = "com.mysql.jdbc.Driver"; try { while(true) { Socket connectionSocket = welcomeSocket.accept(); System.out.println("Connection received from ::: " + connectionSocket.getInetAddress().getHostName()); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); System.out.println("Received: " + clientSentence); capitalizedSentence = clientSentence.toUpperCase() + '\r' + '\n'; outToClient.writeBytes("From Server :: " + capitalizedSentence); Str2 = clientSentence.substring(0,2); System.out.println("First two chars of string::" + Str2 + "Our defined String::" + Str1); if (Str1.equals(Str2)) { System.out.println("The data is valid"); outToClient.writeBytes("From Server :: Valid Data" ); Class.forName(driver); con = DriverManager.getConnection(url + db,"root","root"); Statement st = con.createStatement(); String sql = "INSERT INTO DataReceived(ReceivedData) " + "VALUES ('"+clientSentence+"')"; try{ int val = st.executeUpdate(sql); System.out.println("1 row affected"); } catch (SQLException s){ System.out.println("SQL statement is not executed!"); } } else { System.out.println("The incoming data is Invalid"); outToClient.writeBytes("From Server :: InValid Data" ); } } } catch (Exception ex) { System.out.print("Whoops! It didn't work!\n"); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)