Guest User

Untitled

a guest
Oct 12th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 KB | None | 0 0
  1. java.sql.SQLException: No suitable driver found for jdbc:mysql://database/table
  2. at java.sql.DriverManager.getConnection(DriverManager.java:689)
  3. at java.sql.DriverManager.getConnection(DriverManager.java:247)
  4.  
  5. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
  6.  
  7. java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
  8.  
  9. Context context = new InitialContext();
  10. DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");
  11.  
  12. MysqlDataSource dataSource = new MysqlDataSource();
  13. dataSource.setUser("scott");
  14. dataSource.setPassword("tiger");
  15. dataSource.setServerName("myDBHost.example.org");
  16.  
  17. Connection conn = dataSource.getConnection();
  18. Statement stmt = conn.createStatement();
  19. ResultSet rs = stmt.executeQuery("SELECT ID FROM USERS");
  20. ...
  21. rs.close();
  22. stmt.close();
  23. conn.close();
  24.  
  25. .
  26.  
  27. CREATE DATABASE javabase DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
  28.  
  29. CREATE USER 'java'@'localhost' IDENTIFIED BY 'password';
  30. GRANT ALL ON javabase.* TO 'java'@'localhost' IDENTIFIED BY 'password';
  31.  
  32. hostname
  33.  
  34. main()
  35.  
  36. String url = "jdbc:mysql://localhost:3306/javabase";
  37. String username = "java";
  38. String password = "password";
  39.  
  40. System.out.println("Connecting database...");
  41.  
  42. try (Connection connection = DriverManager.getConnection(url, username, password)) {
  43. System.out.println("Database connected!");
  44. } catch (SQLException e) {
  45. throw new IllegalStateException("Cannot connect the database!", e);
  46. }
  47.  
  48. System.out.println("Loading driver...");
  49.  
  50. try {
  51. Class.forName("com.mysql.jdbc.Driver");
  52. System.out.println("Driver loaded!");
  53. } catch (ClassNotFoundException e) {
  54. throw new IllegalStateException("Cannot find the driver in the classpath!", e);
  55. }
  56.  
  57. // init database constants
  58. private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
  59. private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/database_name";
  60. private static final String USERNAME = "root";
  61. private static final String PASSWORD = "";
  62. private static final String MAX_POOL = "250"; // set your own limit
  63.  
  64. // init connection object
  65. private Connection connection;
  66. // init properties object
  67. private Properties properties;
  68.  
  69. // create properties
  70. private Properties getProperties() {
  71. if (properties == null) {
  72. properties = new Properties();
  73. properties.setProperty("user", USERNAME);
  74. properties.setProperty("password", PASSWORD);
  75. properties.setProperty("MaxPooledStatements", MAX_POOL);
  76. }
  77. return properties;
  78. }
  79.  
  80. // connect database
  81. public Connection connect() {
  82. if (connection == null) {
  83. try {
  84. Class.forName(DATABASE_DRIVER);
  85. connection = DriverManager.getConnection(DATABASE_URL, getProperties());
  86. } catch (ClassNotFoundException | SQLException e) {
  87. // Java 7+
  88. e.printStackTrace();
  89. }
  90. }
  91. return connection;
  92. }
  93.  
  94. // disconnect database
  95. public void disconnect() {
  96. if (connection != null) {
  97. try {
  98. connection.close();
  99. connection = null;
  100. } catch (SQLException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. }
  105.  
  106. import java.sql.Connection;
  107. import java.sql.DriverManager;
  108. import java.sql.SQLException;
  109. import java.util.Properties;
  110.  
  111. public class MysqlConnect {
  112. // init database constants
  113. private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
  114. private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/database_name";
  115. private static final String USERNAME = "root";
  116. private static final String PASSWORD = "";
  117. private static final String MAX_POOL = "250";
  118.  
  119. // init connection object
  120. private Connection connection;
  121. // init properties object
  122. private Properties properties;
  123.  
  124. // create properties
  125. private Properties getProperties() {
  126. if (properties == null) {
  127. properties = new Properties();
  128. properties.setProperty("user", USERNAME);
  129. properties.setProperty("password", PASSWORD);
  130. properties.setProperty("MaxPooledStatements", MAX_POOL);
  131. }
  132. return properties;
  133. }
  134.  
  135. // connect database
  136. public Connection connect() {
  137. if (connection == null) {
  138. try {
  139. Class.forName(DATABASE_DRIVER);
  140. connection = DriverManager.getConnection(DATABASE_URL, getProperties());
  141. } catch (ClassNotFoundException | SQLException e) {
  142. e.printStackTrace();
  143. }
  144. }
  145. return connection;
  146. }
  147.  
  148. // disconnect database
  149. public void disconnect() {
  150. if (connection != null) {
  151. try {
  152. connection.close();
  153. connection = null;
  154. } catch (SQLException e) {
  155. e.printStackTrace();
  156. }
  157. }
  158. }
  159. }
  160.  
  161. // !_ note _! this is just init
  162. // it will not create a connection
  163. MysqlConnect mysqlConnect = new MysqlConnect();
  164.  
  165. String sql = "SELECT * FROM `stackoverflow`";
  166. try {
  167. PreparedStatement statement = mysqlConnect.connect().prepareStatement(sql);
  168. ... go on ...
  169. ... go on ...
  170. ... DONE ....
  171. } catch (SQLException e) {
  172. e.printStackTrace();
  173. } finally {
  174. mysqlConnect.disconnect();
  175. }
  176.  
  177. String url = "jdbc:mysql://127.0.0.1:3306/yourdatabase";
  178. String user = "username";
  179. String password = "password";
  180.  
  181. // Load the Connector/J driver
  182. Class.forName("com.mysql.jdbc.Driver").newInstance();
  183. // Establish connection to MySQL
  184. Connection conn = DriverManager.getConnection(url, user, password);
  185.  
  186. Class.forName("com.mysql.jdbc.Driver").newInstance();
  187. Connection conn = DriverManager.getConnection
  188. ("jdbc:mysql://localhost:3306/foo", "root", "password");
  189.  
  190. Statement stmt = conn.createStatement();
  191. stmt.execute("SELECT * FROM `FOO.BAR`");
  192. stmt.close();
  193. conn.close();
  194.  
  195. private String db_server = BaseMethods.getSystemData("db_server");
  196. private String db_user = BaseMethods.getSystemData("db_user");
  197. private String db_password = BaseMethods.getSystemData("db_password");
  198.  
  199. private String connectToDb() throws Exception {
  200. String jdbcDriver = "com.mysql.jdbc.Driver";
  201. String dbUrl = "jdbc:mysql://" + db_server +
  202. "?verifyServerCertificate=false" +
  203. "&useSSL=true" +
  204. "&requireSSL=true";
  205. System.setProperty(jdbcDriver, "");
  206. Class.forName(jdbcDriver).newInstance();
  207.  
  208. Connection conn = DriverManager.getConnection(dbUrl, db_user, db_password);
  209. Statement statement = conn.createStatement();
  210. String query = "SELECT EXTERNAL_ID FROM offer_letter where ID =" + """ + letterID + """;
  211. ResultSet resultSet = statement.executeQuery(query);
  212. resultSet.next();
  213. return resultSet.getString(1);
  214. }
  215.  
  216. Connection con = DriverManager.getConnection(
  217. "jdbc:myDriver:DatabaseName",
  218. dBuserName,
  219. dBuserPassword);
  220.  
  221. Statement stmt = con.createStatement();
  222. ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table");
  223.  
  224. while (rs.next()) {
  225. int x = rs.getInt("a");
  226. String s = rs.getString("b");
  227. float f = rs.getFloat("c");
  228. }
  229.  
  230. try
  231. {
  232. Class.forName("com.mysql.jdbc.Driver");
  233. System.out.println("Driver Loaded");
  234. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testDB","root","");
  235. //Database Name - testDB, Username - "root", Password - ""
  236. System.out.println("Connected...");
  237. }
  238. catch(Exception e)
  239. {
  240. e.printStackTrace();
  241. }
  242.  
  243. try
  244. {
  245. String url = "jdbc:sqlserver://KHILAN:1433;databaseName=testDB;user=Khilan;password=Tuxedo123";
  246. //KHILAN is Host and 1433 is port number
  247. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  248. System.out.println("Driver Loaded");
  249. conn = DriverManager.getConnection(url);
  250. System.out.println("Connected...");
  251. }
  252. catch(Exception e)
  253. {
  254. e.printStackTrace();
  255. }
  256.  
  257. Connection con = DriverManager.getConnection(
  258. "jdbc:myDriver:DatabaseName",
  259. dBuserName,
  260. dBuserPassword);
  261. if (con != null){
  262. //..handle your code there
  263. }
  264.  
  265. Class.forName("com.mysql.jdbc.Driver");
  266.  
  267. Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseName","Username","Password");
  268. Statement stmt=con.createStatement();
  269. stmt = con.createStatement();
  270. ResultSet rs=stmt.executeQuery("Select * from Table");
  271.  
  272. public class DB {
  273.  
  274. public static Connection c;
  275.  
  276. public static Connection getConnection() throws Exception {
  277. if (c == null) {
  278. Class.forName("com.mysql.jdbc.Driver");
  279. c =DriverManager.getConnection("jdbc:mysql://localhost:3306/DATABASE", "USERNAME", "Password");
  280. }
  281. return c;
  282. }
  283.  
  284. // Send data TO Database
  285. public static void setData(String sql) throws Exception {
  286. DB.getConnection().createStatement().executeUpdate(sql);
  287. }
  288.  
  289. // Get Data From Database
  290. public static ResultSet getData(String sql) throws Exception {
  291. ResultSet rs = DB.getConnection().createStatement().executeQuery(sql);
  292. return rs;
  293. }
  294. }
Add Comment
Please, Sign In to add comment