Advertisement
Guest User

Untitled

a guest
Jan 14th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
  2.  
  3. String url = "jdbc:mysql://localhost:3306/eventhub_test"; //
  4. String user = "root";
  5. String pass = "";
  6.  
  7. SQLUtils sqlu = new SQLUtils(url, user, pass);
  8.  
  9. public class SQLUtils {
  10.  
  11.  
  12. private String CONNECTION_URL;
  13. private String user;
  14. private String pass;
  15. private java.sql.Statement stmt;
  16. private java.sql.Connection conn;
  17.  
  18. public SQLUtils(String conn_url, String user, String pass) {
  19. this.CONNECTION_URL = conn_url;
  20. this.user = user;
  21. this.pass = pass;
  22. }
  23.  
  24.  
  25. public void init() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {
  26.  
  27. Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  28. conn = DriverManager.getConnection(CONNECTION_URL, user, pass);
  29. stmt = conn.createStatement();
  30.  
  31. }
  32. }
  33.  
  34. mysql-connector-java-5.1.7-bin.jar
  35.  
  36. String url = "jdbc:mysql://10.0.2.2/test";
  37.  
  38. public void init() throws IllegalAccessException, InstantiationException,
  39. ClassNotFoundException, SQLException {
  40.  
  41. Class.forName("com.mysql.jdbc.Driver").newInstance();
  42. try {
  43. conn = DriverManager.getConnection(CONNECTION_URL, user, pass);
  44. } catch (java.sql.SQLException e1) {
  45. e1.printStackTrace();
  46. }
  47. try {
  48.  
  49. stmt = conn.createStatement();
  50. ResultSet rs = stmt.executeQuery("SELECT title FROM books");
  51. String entry;
  52. while (rs.next()){
  53. entry = rs.getString(1);
  54. }
  55. rs.close();
  56. stmt.close();
  57. conn.close();
  58. } catch (java.sql.SQLException e) {
  59. e.printStackTrace();
  60. }
  61.  
  62. }
  63.  
  64. import java.sql.Connection;
  65. import java.sql.DriverManager;
  66. import java.sql.ResultSet;
  67. import java.sql.SQLException;
  68. import java.sql.Statement;
  69.  
  70. public void conectarBDMySQL (String user, String password,
  71. String ip, String port, String cat)
  72. {
  73. if (conexionMySQL == null)
  74. {
  75. String urlConexionMySQL = "";
  76. if (cat!= "")
  77. urlConexionMySQL = "jdbc:mysql://" + ip + ":" + port+ "/" + cat;
  78. else
  79. urlConexionMySQL = "jdbc:mysql://" + ip + ":" + port;
  80. if (user!= "" & password!= "" & ip != "" & port!= "")
  81. {
  82. try
  83. {
  84. Class.forName("com.mysql.jdbc.Driver");
  85. conexionMySQL = DriverManager.getConnection(urlConexionMySQL,
  86. user, password);
  87. }
  88. catch (ClassNotFoundException e)
  89. {
  90. Toast.makeText(getApplicationContext(),
  91. "Error: " + e.getMessage(),
  92. Toast.LENGTH_SHORT).show();
  93. }
  94. catch (SQLException e)
  95. {
  96. Toast.makeText(getApplicationContext(),
  97. "Error: " + e.getMessage(),
  98. Toast.LENGTH_SHORT).show();
  99. }
  100. }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement