Guest User

Untitled

a guest
Apr 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.71 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package lmsocketserver;
  7.  
  8. import java.io.*;
  9. import java.net.*;
  10. import java.sql.*;
  11.  
  12.  
  13. /**
  14. *
  15. * @author Jon
  16. */
  17. public class Main {
  18.  
  19. private int port = 49187;
  20. private boolean kill = false;
  21. private BufferedReader in = null;
  22. private boolean askPort = false;
  23.  
  24. //default sqlDB credentials
  25. private String host = "jdbc:mysql://localhost:3306/staging_landscapemafia_com";
  26. private String user = "JDBCConn";
  27. private String pw = "3605texastopaz";
  28.  
  29. Connection conn;
  30.  
  31. /**
  32. * @param args the command line arguments
  33. */
  34. public static void main(String[] args) {
  35. // TODO code application logic here
  36. Main m = new Main();
  37. }
  38. public Main(){
  39.  
  40. try{
  41. dbConnect(host,user,pw);
  42. }
  43. catch(SQLException sqle){
  44. //unnecessary, java makes you put this here. Stupid.
  45. System.out.println("This will never throw an error");
  46. }
  47. if(askPort && conn!=null){
  48. askPort();
  49. }
  50. if(conn!=null){
  51. initSocketLister();
  52. }
  53. else{System.exit(0);}
  54.  
  55. }
  56.  
  57. public void dbConnect(String h,String u,String pwd) throws SQLException{
  58.  
  59.  
  60. System.out.println("attempting to connect to "+h+" as "+u+"("+pwd+")");
  61.  
  62. //System.out.println("starting sql output (under Development)------------");
  63. conn = getOracleJDBCConnection(h, u, pwd);
  64.  
  65. if(conn!= null){
  66. System.out.println("Got Connection.");
  67. DatabaseMetaData meta = conn.getMetaData();
  68. System.out.println("Driver Name : "+meta.getDriverName());
  69. System.out.println("Driver Version : "+meta.getDriverVersion());
  70.  
  71.  
  72.  
  73. //start talking to the sql server
  74. //Statement stmt = conn.prepareStatement("INSERT SQL CODE HERE");
  75.  
  76. }
  77. else{
  78.  
  79. System.out.println("Could not establish connection");
  80. }
  81.  
  82. }
  83.  
  84. public static Connection getOracleJDBCConnection(String h, String u, String p){
  85. Connection conne = null;
  86. try {
  87. //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  88. Class.forName("com.mysql.jdbc.Driver").newInstance();
  89. //conne = DriverManager.getConnection(sqlURL, sqlUser, sqlpw);
  90. //conne = DriverManager.getConnection(sqlURL);
  91. conne = DriverManager.getConnection(h, u, p);
  92. }
  93. catch(SQLException ex){
  94. System.err.println("SQLException: " + ex.getMessage());
  95. System.out.println("SQLState: " + ex.getSQLState());
  96. System.out.println("VendorError: " + ex.getErrorCode());
  97. }
  98. catch(Exception e) {
  99. System.out.println("General Exception: "+e);
  100. }
  101.  
  102. return conne;
  103.  
  104. }
  105. public Connection openConnection(String host,String user,String pw){
  106. Connection temp = null;
  107. try{
  108. temp = DriverManager.getConnection(host, user, pw);
  109. }
  110. catch(Exception e){
  111. System.out.println("openConnection failed");
  112. }
  113.  
  114. return temp;
  115. }
  116. public void disconnect(){
  117. try{
  118. conn.close();
  119. }
  120. catch(SQLException sqle){System.out.println("disconnect error");}
  121. finally{
  122. conn = null;
  123. }
  124.  
  125. }
  126. private void askPort(){
  127. try{
  128. in = new BufferedReader(new InputStreamReader(System.in));//listen for user inputs
  129. }
  130. catch(Exception e){
  131. System.out.println("Error: "+e);
  132. }
  133. System.out.println("-----Activating LM Listener Program-----");
  134. System.out.println("Please select a Port for use (default=49187): \r\n");
  135. boolean moveOn = false;
  136. //get Port number from user
  137. while(!moveOn){
  138. try{
  139. String portS = in.readLine().trim();
  140. System.out.println("Port: "+portS);
  141. port = Integer.parseInt(portS);
  142. if(port>0 && port <65535){
  143. if(port<49152){
  144. System.out.println("This port number may be used by reserved by another program, consider the range 49152–65535");
  145. System.out.println("Use port "+port+" anyway(y/n)");
  146. boolean hold = true;
  147. while(hold){
  148. String answer = in.readLine();
  149. if(answer.equals("y")||answer.equals("Y")){
  150. System.out.println("Port "+port+" confirmed for use.");
  151. moveOn = true;
  152. hold = false;
  153. }
  154. else if(answer.equals("n")||answer.equals("N")){
  155. System.out.println("Please select a Port for use (default=49187): \r\n");
  156. moveOn = false;
  157. hold = false;
  158. }
  159. else{
  160. System.out.println("Invalid input, please type 'y' or 'n'");
  161. }
  162. }
  163. }
  164. else{
  165. System.out.println("Port "+port+" confirmed for use.");
  166. moveOn = true;
  167. }
  168. }
  169. else{
  170. System.out.println("Invalid Port Number, please select a port between 0–65535 (preferably above 49152)");
  171. }
  172. }
  173. catch(NumberFormatException e){System.out.println("Not a valid Port Number, try again...");}
  174. catch(IOException ioe){}
  175. }
  176. }
  177. private void initSocketLister(){
  178. System.out.println("Listening on port "+port+" for connection");
  179. while(!kill){
  180. ServerSocket serverSocket;
  181. Socket clientSocket = null;
  182.  
  183. try {
  184. serverSocket= new ServerSocket(port);
  185. clientSocket = serverSocket.accept();
  186. System.out.println("Connection accepted on Port "+port);
  187. PrintWriter out = new PrintWriter(
  188. clientSocket.getOutputStream(), true);
  189. BufferedReader in = new BufferedReader(
  190. new InputStreamReader(
  191. clientSocket.getInputStream()));
  192. String inputLine, outputLine;
  193.  
  194. // initiate conversation with client
  195. boolean going = true;
  196. //while ((going)&&(conn.isValid(10800))) {
  197. while ((going)&&(conn.isValid(300))) {//300 seconds (5 min) for the isValid call, not the connection timeout.
  198. inputLine = in.readLine();
  199.  
  200. System.out.println("inputLine: "+inputLine);
  201. if((inputLine == null)&&!(inputLine.trim().equals("null"))) ;//hold here until new commands come in
  202. else if(inputLine.equals("peace-out")){
  203. out.println("Severing Connection, peace-out");
  204. System.out.println("Closing Connection, resetting....");
  205. serverSocket = null;
  206. clientSocket = null;
  207. System.out.println("Ready for new connection...");
  208. break;
  209. }
  210. else if(inputLine.equals("Die, Die, Die!")){
  211. out.println("ashes to ashes, dust to dust...");
  212. serverSocket = null;
  213. clientSocket = null;
  214. kill = true;
  215. break;
  216. }
  217. else if (inputLine.equals("nodes, please?")){
  218. //logic here to process the sql Nodes table
  219. try{
  220. Statement stmt = conn.createStatement();
  221. ResultSet rs;
  222.  
  223. rs = stmt.executeQuery("SELECT * FROM `node`");
  224. while ( rs.next() ) {
  225. String type = rs.getString("type");
  226. if((type.equals("landscape_plant"))||(type.equals("garden_plant"))){
  227. out.println(type);
  228. out.println(rs.getString("nid"));
  229. out.println(rs.getString("title"));
  230. //out.println(rs.getString("status"));
  231. }
  232. }
  233. out.println("break");
  234. }
  235. catch(SQLException sqle){
  236. System.out.println("node import failed: "+sqle);
  237. }
  238. }
  239. else if (inputLine.equals("uc_products, please?")){
  240. //logic here to process the sql uc_products table
  241. try{
  242. Statement stmt = conn.createStatement();
  243. ResultSet rs;
  244.  
  245. rs = stmt.executeQuery("SELECT * FROM `uc_products`");
  246. while ( rs.next() ) {
  247. out.println(rs.getString("nid"));
  248. out.println(rs.getString("model"));
  249. }
  250. out.println("break");
  251. }
  252. catch(SQLException sqle){
  253. System.out.println("uc_products import failed: "+sqle);
  254. }
  255. }
  256. else if (inputLine.equals("uc_attribute_options, please?")){
  257. //logic here to process the sql uc_attribute_options table
  258. try{
  259. Statement stmt = conn.createStatement();
  260. ResultSet rs;
  261.  
  262. rs = stmt.executeQuery("SELECT * FROM `uc_attribute_options`");
  263. while ( rs.next() ) {
  264. out.println(rs.getString("aid"));
  265. out.println(rs.getString("oid"));
  266. out.println(rs.getString("name"));
  267. out.println(rs.getString("cost"));
  268. out.println(rs.getString("price"));
  269. //out.println(rs.getString("weight"));//unused currently
  270. out.println(rs.getString("ordering"));
  271. }
  272. out.println("break");
  273. }
  274. catch(SQLException sqle){
  275. System.out.println("uc_attribute_options import failed: "+sqle);
  276. }
  277.  
  278. }
  279. else if (inputLine.equals("content_field_common_names, please?")){
  280. //logic here to process the sql content_field_common_names table
  281. try{
  282. Statement stmt = conn.createStatement();
  283. ResultSet rs;
  284.  
  285. rs = stmt.executeQuery("SELECT * FROM `content_field_common_names`");
  286. while ( rs.next() ) {
  287. out.println(rs.getString("nid"));
  288. out.println(rs.getString("delta"));
  289. out.println(rs.getString("field_common_names_value"));
  290. }
  291. out.println("break");
  292. }
  293. catch(SQLException sqle){
  294. System.out.println("content_field_common_names import failed: "+sqle);
  295. }
  296.  
  297. }
  298. else if (inputLine.equals("content_type_garden_plant, please?")){
  299. //logic here to process the sql content_type_garden_plant table
  300. try{
  301. Statement stmt = conn.createStatement();
  302. ResultSet rs;
  303.  
  304. rs = stmt.executeQuery("SELECT * FROM `content_type_garden_plant`");
  305. while ( rs.next() ) {
  306. out.println(rs.getString("nid"));
  307. out.println(rs.getString("field_veggie_sci_name_value"));
  308. }
  309. out.println("break");
  310. }
  311. catch(SQLException sqle){
  312. System.out.println("content_type_garden_plant import failed: "+sqle);
  313. }
  314.  
  315.  
  316. }
  317. else if (inputLine.equals("term_node, please?")){
  318. //logic here to process the sql term_node table
  319. try{
  320. Statement stmt = conn.createStatement();
  321. ResultSet rs;
  322.  
  323. rs = stmt.executeQuery("SELECT * FROM `term_node`");
  324. while ( rs.next() ) {
  325. out.println(rs.getString("nid"));
  326. out.println(rs.getString("tid"));
  327. }
  328. out.println("break");
  329. }
  330. catch(SQLException sqle){
  331. System.out.println("term_node import failed: "+sqle);
  332. }
  333. }
  334. else if (inputLine.equals("content_field_start_price, please?")){
  335. //logic here to process the sql content_field_start_price table
  336. try{
  337. Statement stmt = conn.createStatement();
  338. ResultSet rs;
  339.  
  340. rs = stmt.executeQuery("SELECT * FROM `content_field_start_price`");
  341. while ( rs.next() ) {
  342. out.println(rs.getString("nid"));
  343. out.println(rs.getString("field_start_price_value"));
  344. }
  345. out.println("break");
  346. }
  347. catch(SQLException sqle){
  348. System.out.println("content_field_start_price import failed: "+sqle);
  349. }
  350. }
  351. else if (inputLine.equals("content_field_raw_price, please?")){
  352. //logic here to process the sql content_field_raw_price table
  353. try{
  354. Statement stmt = conn.createStatement();
  355. ResultSet rs;
  356.  
  357. rs = stmt.executeQuery("SELECT * FROM `content_field_raw_price`");
  358. while ( rs.next() ) {
  359. out.println(rs.getString("nid"));
  360. out.println(rs.getString("field_raw_price_value"));
  361. }
  362. out.println("break");
  363. }
  364. catch(SQLException sqle){
  365. System.out.println("content_field_raw_price import failed: "+sqle);
  366. }
  367. }
  368. else if (inputLine.equals("content_field_raw_ship_price, please?")){
  369. //logic here to process the sql content_field_raw_ship_price table
  370. try{
  371. Statement stmt = conn.createStatement();
  372. ResultSet rs;
  373.  
  374. rs = stmt.executeQuery("SELECT * FROM `content_field_raw_ship_price`");
  375. while ( rs.next() ) {
  376. out.println(rs.getString("nid"));
  377. out.println(rs.getString("field_raw_ship_price_value"));
  378. }
  379. out.println("break");
  380. }
  381. catch(SQLException sqle){
  382. System.out.println("content_field_raw_ship_price import failed: "+sqle);
  383. }
  384. }
  385. else if (inputLine.equals("content_field_start_ship_price, please?")){
  386. //logic here to process the sql content_field_start_ship_price table
  387. try{
  388. Statement stmt = conn.createStatement();
  389. ResultSet rs;
  390.  
  391. rs = stmt.executeQuery("SELECT * FROM `content_field_start_ship_price`");
  392. while ( rs.next() ) {
  393. out.println(rs.getString("nid"));
  394. out.println(rs.getString("field_start_ship_price_value"));
  395. }
  396. out.println("break");
  397. }
  398. catch(SQLException sqle){
  399. System.out.println("content_field_start_ship_price import failed: "+sqle);
  400. }
  401. }
  402. else if (inputLine.equals("content_field_name, please?")){
  403. //logic here to process the sql content_field_name table
  404. try{
  405. Statement stmt = conn.createStatement();
  406. ResultSet rs;
  407.  
  408. rs = stmt.executeQuery("SELECT * FROM `content_field_name`");
  409. while ( rs.next() ) {
  410. out.println(rs.getString("nid"));
  411. out.println(rs.getString("field_name_value"));
  412. }
  413. out.println("break");
  414. }
  415. catch(SQLException sqle){
  416. System.out.println("content_field_name import failed: "+sqle);
  417. }
  418. }
  419. else if (inputLine.equals("content_field_common_name, please?")){
  420. //logic here to process the sql content_field_common_name table
  421. try{
  422. Statement stmt = conn.createStatement();
  423. ResultSet rs;
  424.  
  425. rs = stmt.executeQuery("SELECT * FROM `content_field_common_name`");
  426. while ( rs.next() ) {
  427. out.println(rs.getString("nid"));
  428. out.println(rs.getString("field_common_name_value"));
  429. }
  430. out.println("break");
  431. }
  432. catch(SQLException sqle){
  433. System.out.println("content_field_common_name import failed: "+sqle);
  434. }
  435. }
  436. else if (inputLine.equals("lm_growers, please?")){
  437. //logic here to process the sql content_field_common_name table
  438. try{
  439. Statement stmt = conn.createStatement();
  440. ResultSet rs;
  441.  
  442. rs = stmt.executeQuery("SELECT * FROM `lm_growers`");
  443. while ( rs.next() ) {
  444. out.println(rs.getString("gid"));
  445. out.println(rs.getString("name"));
  446. out.println(rs.getString("address"));
  447. }
  448. out.println("break");
  449. }
  450. catch(SQLException sqle){
  451. System.out.println("lm_growers import failed: "+sqle);
  452. }
  453. }
  454. else if (inputLine.equals("lm_records, please?")){
  455. //logic here to process the sql content_field_common_name table
  456. try{
  457. Statement stmt = conn.createStatement();
  458. ResultSet rs;
  459.  
  460. rs = stmt.executeQuery("SELECT * FROM `lm_records`");
  461. while ( rs.next() ) {
  462. //primary keys
  463. out.println(rs.getString("nid"));
  464. out.println(rs.getString("aid"));
  465. out.println(rs.getString("gid"));
  466. //extra info
  467. out.println(rs.getString("cost"));
  468. out.println(rs.getString("price"));
  469. out.println(rs.getString("markup"));
  470. out.println(rs.getString("weight"));
  471. out.println(rs.getString("ordering"));
  472. out.println(rs.getString("active"));
  473. out.println(rs.getString("stock"));
  474. out.println(rs.getString("threshold"));
  475.  
  476. }
  477. out.println("break");
  478. }
  479. catch(SQLException sqle){
  480. System.out.println("lm_records import failed: "+sqle);
  481. }
  482. }
  483. //process queries now
  484. else if (inputLine.equals("query processing, please?")){
  485. //logic here to process the sql content_field_common_name table
  486. try{
  487. out.println("go ahead with query");
  488. boolean queryLoop = true;
  489. String newQ = in.readLine();
  490. while(queryLoop){
  491. Statement stmt = conn.createStatement();
  492. ResultSet rs = stmt.executeQuery(newQ);
  493.  
  494. newQ = in.readLine();
  495. if(newQ.equals("finished querying")){
  496. queryLoop = false;
  497. }
  498. }
  499.  
  500. out.println("break");
  501. }
  502. catch(SQLException sqle){
  503. System.out.println("querying failed: "+sqle);
  504. }
  505. }
  506. else{
  507. System.out.println("I'm not listening... "+inputLine);
  508. }
  509. }
  510. }
  511. catch(BindException be){//throws this several times when abruptly disconnected, no worries
  512. //System.out.println("BindException: "+be+"\r\n--->Address already used. Resetting listener...");
  513. serverSocket = null;
  514. clientSocket = null;
  515. }
  516. catch(IOException e) {
  517. System.out.println("Connection dead: "+e);
  518. serverSocket = null;
  519. clientSocket = null;
  520. //System.exit(-1);
  521. }
  522. catch(SQLException sqle){
  523. System.out.println("Error with timeout function: "+sqle);
  524. }
  525. }
  526. System.exit(0);
  527. }
  528.  
  529. }
Add Comment
Please, Sign In to add comment