Guest User

Untitled

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