Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- java.sql.SQLException: No suitable driver found for jdbc:mysql://database/table
- at java.sql.DriverManager.getConnection(DriverManager.java:689)
- at java.sql.DriverManager.getConnection(DriverManager.java:247)
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
- Context context = new InitialContext();
- DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");
- MysqlDataSource dataSource = new MysqlDataSource();
- dataSource.setUser("scott");
- dataSource.setPassword("tiger");
- dataSource.setServerName("myDBHost.example.org");
- Connection conn = dataSource.getConnection();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT ID FROM USERS");
- ...
- rs.close();
- stmt.close();
- conn.close();
- .
- CREATE DATABASE javabase DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
- CREATE USER 'java'@'localhost' IDENTIFIED BY 'password';
- GRANT ALL ON javabase.* TO 'java'@'localhost' IDENTIFIED BY 'password';
- hostname
- main()
- String url = "jdbc:mysql://localhost:3306/javabase";
- String username = "java";
- String password = "password";
- System.out.println("Connecting database...");
- try (Connection connection = DriverManager.getConnection(url, username, password)) {
- System.out.println("Database connected!");
- } catch (SQLException e) {
- throw new IllegalStateException("Cannot connect the database!", e);
- }
- System.out.println("Loading driver...");
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Driver loaded!");
- } catch (ClassNotFoundException e) {
- throw new IllegalStateException("Cannot find the driver in the classpath!", e);
- }
- // init database constants
- private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
- private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/database_name";
- private static final String USERNAME = "root";
- private static final String PASSWORD = "";
- private static final String MAX_POOL = "250"; // set your own limit
- // init connection object
- private Connection connection;
- // init properties object
- private Properties properties;
- // create properties
- private Properties getProperties() {
- if (properties == null) {
- properties = new Properties();
- properties.setProperty("user", USERNAME);
- properties.setProperty("password", PASSWORD);
- properties.setProperty("MaxPooledStatements", MAX_POOL);
- }
- return properties;
- }
- // connect database
- public Connection connect() {
- if (connection == null) {
- try {
- Class.forName(DATABASE_DRIVER);
- connection = DriverManager.getConnection(DATABASE_URL, getProperties());
- } catch (ClassNotFoundException | SQLException e) {
- // Java 7+
- e.printStackTrace();
- }
- }
- return connection;
- }
- // disconnect database
- public void disconnect() {
- if (connection != null) {
- try {
- connection.close();
- connection = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Properties;
- public class MysqlConnect {
- // init database constants
- private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
- private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/database_name";
- private static final String USERNAME = "root";
- private static final String PASSWORD = "";
- private static final String MAX_POOL = "250";
- // init connection object
- private Connection connection;
- // init properties object
- private Properties properties;
- // create properties
- private Properties getProperties() {
- if (properties == null) {
- properties = new Properties();
- properties.setProperty("user", USERNAME);
- properties.setProperty("password", PASSWORD);
- properties.setProperty("MaxPooledStatements", MAX_POOL);
- }
- return properties;
- }
- // connect database
- public Connection connect() {
- if (connection == null) {
- try {
- Class.forName(DATABASE_DRIVER);
- connection = DriverManager.getConnection(DATABASE_URL, getProperties());
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- }
- }
- return connection;
- }
- // disconnect database
- public void disconnect() {
- if (connection != null) {
- try {
- connection.close();
- connection = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- // !_ note _! this is just init
- // it will not create a connection
- MysqlConnect mysqlConnect = new MysqlConnect();
- String sql = "SELECT * FROM `stackoverflow`";
- try {
- PreparedStatement statement = mysqlConnect.connect().prepareStatement(sql);
- ... go on ...
- ... go on ...
- ... DONE ....
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- mysqlConnect.disconnect();
- }
- String url = "jdbc:mysql://127.0.0.1:3306/yourdatabase";
- String user = "username";
- String password = "password";
- // Load the Connector/J driver
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- // Establish connection to MySQL
- Connection conn = DriverManager.getConnection(url, user, password);
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection conn = DriverManager.getConnection
- ("jdbc:mysql://localhost:3306/foo", "root", "password");
- Statement stmt = conn.createStatement();
- stmt.execute("SELECT * FROM `FOO.BAR`");
- stmt.close();
- conn.close();
- private String db_server = BaseMethods.getSystemData("db_server");
- private String db_user = BaseMethods.getSystemData("db_user");
- private String db_password = BaseMethods.getSystemData("db_password");
- private String connectToDb() throws Exception {
- String jdbcDriver = "com.mysql.jdbc.Driver";
- String dbUrl = "jdbc:mysql://" + db_server +
- "?verifyServerCertificate=false" +
- "&useSSL=true" +
- "&requireSSL=true";
- System.setProperty(jdbcDriver, "");
- Class.forName(jdbcDriver).newInstance();
- Connection conn = DriverManager.getConnection(dbUrl, db_user, db_password);
- Statement statement = conn.createStatement();
- String query = "SELECT EXTERNAL_ID FROM offer_letter where ID =" + """ + letterID + """;
- ResultSet resultSet = statement.executeQuery(query);
- resultSet.next();
- return resultSet.getString(1);
- }
- Connection con = DriverManager.getConnection(
- "jdbc:myDriver:DatabaseName",
- dBuserName,
- dBuserPassword);
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table");
- while (rs.next()) {
- int x = rs.getInt("a");
- String s = rs.getString("b");
- float f = rs.getFloat("c");
- }
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Driver Loaded");
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testDB","root","");
- //Database Name - testDB, Username - "root", Password - ""
- System.out.println("Connected...");
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- try
- {
- String url = "jdbc:sqlserver://KHILAN:1433;databaseName=testDB;user=Khilan;password=Tuxedo123";
- //KHILAN is Host and 1433 is port number
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- System.out.println("Driver Loaded");
- conn = DriverManager.getConnection(url);
- System.out.println("Connected...");
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- Connection con = DriverManager.getConnection(
- "jdbc:myDriver:DatabaseName",
- dBuserName,
- dBuserPassword);
- if (con != null){
- //..handle your code there
- }
- Class.forName("com.mysql.jdbc.Driver");
- Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseName","Username","Password");
- Statement stmt=con.createStatement();
- stmt = con.createStatement();
- ResultSet rs=stmt.executeQuery("Select * from Table");
- public class DB {
- public static Connection c;
- public static Connection getConnection() throws Exception {
- if (c == null) {
- Class.forName("com.mysql.jdbc.Driver");
- c =DriverManager.getConnection("jdbc:mysql://localhost:3306/DATABASE", "USERNAME", "Password");
- }
- return c;
- }
- // Send data TO Database
- public static void setData(String sql) throws Exception {
- DB.getConnection().createStatement().executeUpdate(sql);
- }
- // Get Data From Database
- public static ResultSet getData(String sql) throws Exception {
- ResultSet rs = DB.getConnection().createStatement().executeQuery(sql);
- return rs;
- }
- }
Add Comment
Please, Sign In to add comment