Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package lmsocketserver;
- import java.io.*;
- import java.net.*;
- import java.sql.*;
- /**
- *
- * @author Jon
- */
- public class Main {
- private int port = 49187;
- private boolean kill = false;
- private BufferedReader in = null;
- private boolean askPort = false;
- //default sqlDB credentials
- private String host = "jdbc:mysql://localhost:3306/staging_landscapemafia_com";
- private String user = "JDBCConn";
- private String pw = "3605texastopaz";
- Connection conn;
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Main m = new Main();
- }
- public Main(){
- try{
- dbConnect(host,user,pw);
- }
- catch(SQLException sqle){
- //unnecessary, java makes you put this here. Stupid.
- System.out.println("This will never throw an error");
- }
- if(askPort && conn!=null){
- askPort();
- }
- if(conn!=null){
- initSocketLister();
- }
- else{System.exit(0);}
- }
- public void dbConnect(String h,String u,String pwd) throws SQLException{
- System.out.println("attempting to connect to "+h+" as "+u+"("+pwd+")");
- //System.out.println("starting sql output (under Development)------------");
- conn = getOracleJDBCConnection(h, u, pwd);
- if(conn!= null){
- System.out.println("Got Connection.");
- DatabaseMetaData meta = conn.getMetaData();
- System.out.println("Driver Name : "+meta.getDriverName());
- System.out.println("Driver Version : "+meta.getDriverVersion());
- //start talking to the sql server
- //Statement stmt = conn.prepareStatement("INSERT SQL CODE HERE");
- }
- else{
- System.out.println("Could not establish connection");
- }
- }
- public static Connection getOracleJDBCConnection(String h, String u, String p){
- Connection conne = null;
- try {
- //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- //conne = DriverManager.getConnection(sqlURL, sqlUser, sqlpw);
- //conne = DriverManager.getConnection(sqlURL);
- conne = DriverManager.getConnection(h, u, p);
- }
- catch(SQLException ex){
- System.err.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- }
- catch(Exception e) {
- System.out.println("General Exception: "+e);
- }
- return conne;
- }
- public Connection openConnection(String host,String user,String pw){
- Connection temp = null;
- try{
- temp = DriverManager.getConnection(host, user, pw);
- }
- catch(Exception e){
- System.out.println("openConnection failed");
- }
- return temp;
- }
- public void disconnect(){
- try{
- conn.close();
- }
- catch(SQLException sqle){System.out.println("disconnect error");}
- finally{
- conn = null;
- }
- }
- private void askPort(){
- try{
- in = new BufferedReader(new InputStreamReader(System.in));//listen for user inputs
- }
- catch(Exception e){
- System.out.println("Error: "+e);
- }
- System.out.println("-----Activating LM Listener Program-----");
- System.out.println("Please select a Port for use (default=49187): \r\n");
- boolean moveOn = false;
- //get Port number from user
- while(!moveOn){
- try{
- String portS = in.readLine().trim();
- System.out.println("Port: "+portS);
- port = Integer.parseInt(portS);
- if(port>0 && port <65535){
- if(port<49152){
- System.out.println("This port number may be used by reserved by another program, consider the range 49152–65535");
- System.out.println("Use port "+port+" anyway(y/n)");
- boolean hold = true;
- while(hold){
- String answer = in.readLine();
- if(answer.equals("y")||answer.equals("Y")){
- System.out.println("Port "+port+" confirmed for use.");
- moveOn = true;
- hold = false;
- }
- else if(answer.equals("n")||answer.equals("N")){
- System.out.println("Please select a Port for use (default=49187): \r\n");
- moveOn = false;
- hold = false;
- }
- else{
- System.out.println("Invalid input, please type 'y' or 'n'");
- }
- }
- }
- else{
- System.out.println("Port "+port+" confirmed for use.");
- moveOn = true;
- }
- }
- else{
- System.out.println("Invalid Port Number, please select a port between 0–65535 (preferably above 49152)");
- }
- }
- catch(NumberFormatException e){System.out.println("Not a valid Port Number, try again...");}
- catch(IOException ioe){}
- }
- }
- private void initSocketLister(){
- System.out.println("Listening on port "+port+" for connection");
- while(!kill){
- ServerSocket serverSocket;
- Socket clientSocket = null;
- try {
- serverSocket= new ServerSocket(port);
- clientSocket = serverSocket.accept();
- System.out.println("Connection accepted on Port "+port);
- PrintWriter out = new PrintWriter(
- clientSocket.getOutputStream(), true);
- BufferedReader in = new BufferedReader(
- new InputStreamReader(
- clientSocket.getInputStream()));
- String inputLine, outputLine;
- // initiate conversation with client
- boolean going = true;
- //while ((going)&&(conn.isValid(10800))) {
- while ((going)&&(conn.isValid(300))) {//300 seconds (5 min) for the isValid call, not the connection timeout.
- inputLine = in.readLine();
- System.out.println("inputLine: "+inputLine);
- if((inputLine == null)&&!(inputLine.trim().equals("null"))) ;//hold here until new commands come in
- else if(inputLine.equals("peace-out")){
- out.println("Severing Connection, peace-out");
- System.out.println("Closing Connection, resetting....");
- serverSocket = null;
- clientSocket = null;
- System.out.println("Ready for new connection...");
- break;
- }
- else if(inputLine.equals("Die, Die, Die!")){
- out.println("ashes to ashes, dust to dust...");
- serverSocket = null;
- clientSocket = null;
- kill = true;
- break;
- }
- else if (inputLine.equals("nodes, please?")){
- //logic here to process the sql Nodes table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `node`");
- while ( rs.next() ) {
- String type = rs.getString("type");
- if((type.equals("landscape_plant"))||(type.equals("garden_plant"))){
- out.println(type);
- out.println(rs.getString("nid"));
- out.println(rs.getString("title"));
- //out.println(rs.getString("status"));
- }
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("node import failed: "+sqle);
- }
- }
- else if (inputLine.equals("uc_products, please?")){
- //logic here to process the sql uc_products table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `uc_products`");
- while ( rs.next() ) {
- out.println(rs.getString("nid"));
- out.println(rs.getString("model"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("uc_products import failed: "+sqle);
- }
- }
- else if (inputLine.equals("uc_attribute_options, please?")){
- //logic here to process the sql uc_attribute_options table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `uc_attribute_options`");
- while ( rs.next() ) {
- out.println(rs.getString("aid"));
- out.println(rs.getString("oid"));
- out.println(rs.getString("name"));
- out.println(rs.getString("cost"));
- out.println(rs.getString("price"));
- //out.println(rs.getString("weight"));//unused currently
- out.println(rs.getString("ordering"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("uc_attribute_options import failed: "+sqle);
- }
- }
- else if (inputLine.equals("content_field_common_names, please?")){
- //logic here to process the sql content_field_common_names table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `content_field_common_names`");
- while ( rs.next() ) {
- out.println(rs.getString("nid"));
- out.println(rs.getString("delta"));
- out.println(rs.getString("field_common_names_value"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("content_field_common_names import failed: "+sqle);
- }
- }
- else if (inputLine.equals("content_type_garden_plant, please?")){
- //logic here to process the sql content_type_garden_plant table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `content_type_garden_plant`");
- while ( rs.next() ) {
- out.println(rs.getString("nid"));
- out.println(rs.getString("field_veggie_sci_name_value"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("content_type_garden_plant import failed: "+sqle);
- }
- }
- else if (inputLine.equals("term_node, please?")){
- //logic here to process the sql term_node table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `term_node`");
- while ( rs.next() ) {
- out.println(rs.getString("nid"));
- out.println(rs.getString("tid"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("term_node import failed: "+sqle);
- }
- }
- else if (inputLine.equals("content_field_start_price, please?")){
- //logic here to process the sql content_field_start_price table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `content_field_start_price`");
- while ( rs.next() ) {
- out.println(rs.getString("nid"));
- out.println(rs.getString("field_start_price_value"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("content_field_start_price import failed: "+sqle);
- }
- }
- else if (inputLine.equals("content_field_raw_price, please?")){
- //logic here to process the sql content_field_raw_price table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `content_field_raw_price`");
- while ( rs.next() ) {
- out.println(rs.getString("nid"));
- out.println(rs.getString("field_raw_price_value"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("content_field_raw_price import failed: "+sqle);
- }
- }
- else if (inputLine.equals("content_field_raw_ship_price, please?")){
- //logic here to process the sql content_field_raw_ship_price table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `content_field_raw_ship_price`");
- while ( rs.next() ) {
- out.println(rs.getString("nid"));
- out.println(rs.getString("field_raw_ship_price_value"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("content_field_raw_ship_price import failed: "+sqle);
- }
- }
- else if (inputLine.equals("content_field_start_ship_price, please?")){
- //logic here to process the sql content_field_start_ship_price table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `content_field_start_ship_price`");
- while ( rs.next() ) {
- out.println(rs.getString("nid"));
- out.println(rs.getString("field_start_ship_price_value"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("content_field_start_ship_price import failed: "+sqle);
- }
- }
- else if (inputLine.equals("content_field_name, please?")){
- //logic here to process the sql content_field_name table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `content_field_name`");
- while ( rs.next() ) {
- out.println(rs.getString("nid"));
- out.println(rs.getString("field_name_value"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("content_field_name import failed: "+sqle);
- }
- }
- else if (inputLine.equals("content_field_common_name, please?")){
- //logic here to process the sql content_field_common_name table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `content_field_common_name`");
- while ( rs.next() ) {
- out.println(rs.getString("nid"));
- out.println(rs.getString("field_common_name_value"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("content_field_common_name import failed: "+sqle);
- }
- }
- else if (inputLine.equals("lm_growers, please?")){
- //logic here to process the sql content_field_common_name table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `lm_growers`");
- while ( rs.next() ) {
- out.println(rs.getString("gid"));
- out.println(rs.getString("name"));
- out.println(rs.getString("address"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("lm_growers import failed: "+sqle);
- }
- }
- else if (inputLine.equals("lm_records, please?")){
- //logic here to process the sql content_field_common_name table
- try{
- Statement stmt = conn.createStatement();
- ResultSet rs;
- rs = stmt.executeQuery("SELECT * FROM `lm_records`");
- while ( rs.next() ) {
- //primary keys
- out.println(rs.getString("nid"));
- out.println(rs.getString("aid"));
- out.println(rs.getString("gid"));
- //extra info
- out.println(rs.getString("cost"));
- out.println(rs.getString("price"));
- out.println(rs.getString("markup"));
- out.println(rs.getString("weight"));
- out.println(rs.getString("ordering"));
- out.println(rs.getString("active"));
- out.println(rs.getString("stock"));
- out.println(rs.getString("threshold"));
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("lm_records import failed: "+sqle);
- }
- }
- //process queries now
- else if (inputLine.equals("query processing, please?")){
- //logic here to process the sql content_field_common_name table
- try{
- out.println("go ahead with query");
- boolean queryLoop = true;
- String newQ = in.readLine();
- while(queryLoop){
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(newQ);
- newQ = in.readLine();
- if(newQ.equals("finished querying")){
- queryLoop = false;
- }
- }
- out.println("break");
- }
- catch(SQLException sqle){
- System.out.println("querying failed: "+sqle);
- }
- }
- else{
- System.out.println("I'm not listening... "+inputLine);
- }
- }
- }
- catch(BindException be){//throws this several times when abruptly disconnected, no worries
- //System.out.println("BindException: "+be+"\r\n--->Address already used. Resetting listener...");
- serverSocket = null;
- clientSocket = null;
- }
- catch(IOException e) {
- System.out.println("Connection dead: "+e);
- serverSocket = null;
- clientSocket = null;
- //System.exit(-1);
- }
- catch(SQLException sqle){
- System.out.println("Error with timeout function: "+sqle);
- }
- }
- System.exit(0);
- }
- }
Add Comment
Please, Sign In to add comment