Setup mysql datasource : MySQL : Database SQL JDBC : Java examples (example source code) Organized by topic

Java
C++
PHP
Java Home »  Database SQL JDBC   » [  MySQL  ]  Screenshots 
 



Setup mysql datasource

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import com.mysql.jdbc.jdbc2.optional.*;
import javax.sql.*;
import javax.naming.*;
import java.util.*;

public class SetupJNDIDataSource {
  public static void main(String args[]) {
    try {
      startRegistry();
      ConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
      dataSource.setUser("username");
      dataSource.setPassword("password");
      dataSource.setServerName("localhost");
      dataSource.setPort(3306);
      dataSource.setDatabaseName("databasename");

      InitialContext context = createContext();
      context.rebind("HrDS", dataSource);

    catch (Exception e) {
      System.out.println("SetupJNDIDataSource err: " + e.getMessage());
      e.printStackTrace();
    }
  }

  private static void startRegistry() throws RemoteException {
    LocateRegistry.createRegistry(1099);
    System.out.println("RMI registry ready.");
  }

  private static InitialContext createContext() throws NamingException {
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
    env.put(Context.PROVIDER_URL, "rmi://localhost:1099");
    InitialContext context = new InitialContext(env);
    return context;
  }
}

           
       
Related examples in the same category
1.  Use Oracle DataSource To Store MySql ConnectionHas Download File
2.  Create Database for MySQL
3.  Test MySQL JDBC Driver Installation
4.  Create table for mysql database
5.  Access MySQL Database: open connection, create table, insert and retrieve
6.  Count rows in MySQL
7.  Demo ResultSet for MySQL
8.  Create Table With All Data Types In MySQL
9.  Insert text file into MySQL
10.  Read a Clob object from MySQL
11.  How to serialize/de-serialize a Java object to the MySQL database.
12.  Check JDBC Installation for MySQL
13.  Demo MySql Transaction
14.  Create database for MySql








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.