This tutorial provides an example on how to create a Database using JDBC application. Before executing following example, make sure you have the following in place:
You should have admin privilege to create a database in the given schema. To execute the following example you need to replace username and password with your actual user name and password.
Your MySQL or whatever database you are using is up and running.
Required Steps:
There are following steps required to create a new Database using JDBC application:
Import the packages . Requires that you include the packages containing the JDBC classes needed for database programming. Most often, using import java.sql.* will suffice.
Register the JDBC driver . Requires that you initialize a driver so you can open a communications channel with the database.
Open a connection . Requires using the DriverManager.getConnection() method to create a Connection object, which represents a physical connection with datbase server.
To create a new database, you need not to give any database name while preparing database URL as mentioned in the below example.
Execute a query . Requires using an object of type Statement for building and submitting an SQL statement to the database.
Clean up the environment . Requires explicitly closing all database resources versus relying on the JVM's garbage collection.
Sample Code:
Copy and past following example in JDBCExample.java, compile and run as follows:
//STEP 1. Import required packages
import java.sql.*;
public class JDBCExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//STEP 4: Execute a query
System.out.println("Creating database...");
stmt = conn.createStatement();
String sql = "CREATE DATABASE STUDENTS";
stmt.executeUpdate(sql);
System.out.println("Database created successfully...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample
Now let us compile above example as follows:
C:\>javac JDBCExample.java
C:\>
When you run JDBCExample, it produces following result:
C:\>java JDBCExample
Connecting to database...
Creating database...
Database created successfully...
Goodbye!
C:\>
Thursday, 25 July 2013
Friday, 19 July 2013
SEO SERVICES
April 9, 2013
Think of a website which has a very tempting look. The website has a very lucrative design, arresting appearance and dazzling gaze but no one visits it. All the hard work done and funds invested on website design goes in vain without traffic. To overcome this dilemma, read more
TIPS FOR PHP DEVELOPER
June 6, 2013
A) GO OOP
If you have not yet entered the realm of Object Oriented Programming, then you are at a disadvantage, and you are falling behind fast.
OOP is essentially a method of programming with the use of classes, or Objects, which tie like things together, remove the need for repetition of code and perform the basic tasks of production very simply. Objects are essentially classes that collect a bunch of functions together and wrap them in a wrapper that can be reused over and over again without the need to rewrite functionality or procedures every time you need to do something.
Procedural Programming works by following a routine from the top to the bottom of each page as the server reads every file on your server. With OOP, there could be one or two objects being instantiated, which, in turn could instantiate a few, a hundred or a thousand other objects which could all perform certain tasks depending on variables passed into the objects. OOP is faster, simpler, easier to debug, uses less server resources, less code, is faster loading and more logical to work with once you figure out the basic principles. Go OOP – It changed my development style forever.
B) STAY AWAY FROM ANYTHING ENDING WITH _ONCE()
We all know that include() simply gives us a warning if it fails, while require() kills the script with a fatal error when it fails.
Wednesday, 10 July 2013
Cross-browser vs. multi-browser
Cross-browser vs. multi-browser with regard to scripts, which is the
most common usage, the term cross-browser is often confused with
multi-browser. Multi-browser scripts can only be expected to work in
environments where they have been demonstrated to work (due to
assumptions based on observing a subset of browsers). Most publicly
available libraries and frameworks are multi-browser scripts and list
the environments (typically popular browsers in use at the time and in
their default configurations) where they can be expected to work.
Multi-browser scripts virtually always approach obsolescence as new
browsers are introduced, features are deprecated and removed, and the
authors assumptions are invalidated; therefore, multi-browser scripts
have always required periodic maintenance. As the number of browsers and
configurations in use has grown, so has the frequency of such
maintenance. Older (or otherwise lesser) browsers and browser versions
are periodically dropped as supported environments, regardless of
whether or not they are still in use and without concern for what the
new scripts will do when exposed to these environments.
read more
read more
Subscribe to:
Posts (Atom)