Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- 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();
- // 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();
- }
- 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");
- }
- Connection con = DriverManager.getConnection(
- "jdbc:myDriver:DatabaseName",
- dBuserName,
- dBuserPassword);
- if (con != null){
- //..handle your code there
- }
- 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();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement