Guest User

Untitled

a guest
Aug 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. Java program that uses SQL - compile into .jar
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.util.Scanner;
  6. import com.mysql.jdbc.PreparedStatement;
  7.  
  8.  
  9. public class Main
  10. {
  11. private static Scanner scn = new Scanner(System.in);
  12.  
  13. public static void main(String[] argv) throws Exception
  14. {
  15. Class.forName("com.mysql.jdbc.Driver");
  16.  
  17. Main main = new Main();
  18. int choice = 0;
  19.  
  20. System.out.println("ttWelcome !nn");
  21.  
  22. System.out.print(
  23. "1. Get Data From the table "testtable"n"+
  24. "2. Insert data into the table "testtable"n"+
  25. "Your choice?: "
  26. );
  27. choice = scn.nextInt();
  28. scn.nextLine();
  29.  
  30. switch( choice )
  31. {
  32. case 1:
  33. main.getInfo();
  34. break;
  35.  
  36. case 2:
  37. main.insertInfo();
  38. break;
  39.  
  40. default:
  41. System.out.print("Was neither 1 or 2. Terminating program...");
  42. }
  43.  
  44.  
  45. }
  46.  
  47. private void getInfo() throws Exception
  48. {
  49. //Class.forName("com.mysql.jdbc.Driver");
  50.  
  51. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "guest", "shahin33");
  52. PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT * FROM testtable");
  53. ResultSet result = statement.executeQuery();
  54.  
  55. System.out.println();
  56.  
  57. System.out.println("USERNAMEtPASSWORD");
  58. while( result.next() )
  59. {
  60. System.out.println(result.getString(1) + "tt" + result.getString(2));
  61. }
  62.  
  63. result.close();
  64. con.close();
  65. }
  66.  
  67. private void insertInfo() throws Exception
  68. {
  69. System.out.print("nUsername: ");
  70. String username = scn.nextLine();
  71.  
  72. System.out.print("Password: ");
  73. String password = scn.nextLine().toLowerCase();
  74.  
  75. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "guest", "shahin33");
  76. PreparedStatement statement = (PreparedStatement) con.prepareStatement("INSERT INTO testtable VALUES(?,?)");
  77. statement.setString(1, username);
  78. statement.setString(2, password);
  79. statement.executeUpdate();
  80.  
  81. System.out.println("nUser information has been inserted to the database.nRun the program again and chose 1 to see result.");
  82. statement.close();
  83. con.close();
  84.  
  85. }
  86. }
  87.  
  88. @echo off
  89.  
  90. set /p fileName="Name for the file? Exclude .jar: "
  91.  
  92. copy *.java "Source Code"
  93. javac *.java
  94.  
  95. jar -cvfm %fileName%.jar manifest.txt *.class "Source Code"
  96.  
  97. del *.class /f /q
  98. del "Source Code" /f /q
  99.  
  100. C:UsersshahinDesktopJavaComp>Compile_N_Whatnot.bat
  101. Name for the file? Exclude .jar: comonXXXXXX
  102. Main.java
  103. 1 fil(er) kopierad(e).
  104. Main.java:5: error: package com.mysql.jdbc does not exist
  105. import com.mysql.jdbc.PreparedStatement;
  106. ^
  107. Main.java:51: error: cannot find symbol
  108. PreparedStatement statement = (PreparedStatement) con.prepa
  109. tement("SELECT * FROM testtable");
  110. ^
  111. symbol: class PreparedStatement
  112. location: class Main
  113. Main.java:51: error: cannot find symbol
  114. PreparedStatement statement = (PreparedStatement) con.prepa
  115. tement("SELECT * FROM testtable");
  116. ^
  117. symbol: class PreparedStatement
  118. location: class Main
  119. Main.java:75: error: cannot find symbol
  120. PreparedStatement statement = (PreparedStatement) con.prepa
  121. tement("INSERT INTO testtable VALUES(?,?)");
  122. ^
  123. symbol: class PreparedStatement
  124. location: class Main
  125. Main.java:75: error: cannot find symbol
  126. PreparedStatement statement = (PreparedStatement) con.prepa
  127. tement("INSERT INTO testtable VALUES(?,?)");
  128.  
  129. C:ProgramJavajdk1.7.bin;
  130. C:UsersshahinDesktopmysql-connector-java-5.1.18commysqljdbc;
  131. C:UsersshahinDesktopmysql-connector-java-5.1.18mysql-connector-java-5.1.18-bin.jar;
Add Comment
Please, Sign In to add comment