Home Tutorial Java Dbcp Creating database using DBCP

 
 

Share on Google+Share on Google+
Creating database using DBCP
Posted on: April 1, 2010 at 12:00 AM
Advertisement
This tutorial demonstrate how to create database in MySQL using DBCP.

Code:

CreatingDB.java

import java.sql.*;
import java.io.*;
import org.apache.commons.dbcp.BasicDataSource;

public class CreatingDB {
public static void main(String[] args) {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql://localhost:3306/");
bds.setUsername("root");
bds.setPassword("");
try {
System.out.println("Trying to connect!!");
Connection con = bds.getConnection();
System.out.println("Connected sucessfully !!");
try {
Statement st = con.createStatement();
BufferedReader bf = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("Enter Database name:");
String database = bf.readLine();
st.executeUpdate("CREATE DATABASE " + database);
System.out.println("1 row(s) affacted");
con.close();
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Directory structure of files

In the lib folder place all required file ie. commons-collections.jar, commons-dbcp.jar, commons-pool.jar, j2ee.jar and mysql-connector-java-5.1.7-bin.jar

In the pathset.bat write following statement

set classpath=.;lib/commons-collections.jar;lib/commons-dbcp.jar;lib/commons-pool.jar;lib/j2ee.jar;lib/mysql-connector-java-5.1.7-bin.jar

Output:

Advertisement

Related Tags for Creating database using DBCP:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 1, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com