NEWS & UPDATES >> BCA BCSP064 Synopsis & Project Work Started for DEC 2017, IGNOU MCA MCSP060 Synopsis Work Started for DEC 2017, CONTACT 4 IGNOU Mini Project

IGNOU BCA MCA - PROJECT VIVA Question Answer for JAVA - JSP Part XIV

IGNOU BCA MCA - PROJECT VIVA Question Answer for JAVA - JSP

JAVA - JSP Selected Question Answer - PART XIV

DATABASE CONNECTION WITH JAVA & MY SQL


116. Data base connection code example in java with my sql
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// Notice, do not import com.mysql.jdbc.*
// or you will have problems!

public class LoadDriver {
    public static void main(String[] args) {
        try {
            // The newInstance() call is a work around for some
            // broken Java implementations

            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            // handle the error
        }
    }
}

This example shows how you can obtain a Connection instance from the DriverManager. There are a few different signatures for the getConnection() method. Consult the API documentation that comes with your JDK for more specific information on how to use them.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
Connection conn = null;
...
try {
    conn =
       DriverManager.getConnection("jdbc:mysql://localhost/test?" +
                                   "user=monty&password=greatsqldb");
 
    // Do something with the Connection
 
   ...
} catch (SQLException ex) {
    // handle any errors
    System.out.println("SQLException: " + ex.getMessage());
    System.out.println("SQLState: " + ex.getSQLState());
    System.out.println("VendorError: " + ex.getErrorCode());
}

DATABASE CONNECTION WITH JAVA & MY ACCESS

116. Data base connection code example in java with Ms Access
 It is quiet easy to connect MS Access database through java program. Before proceeding with the program, let me explain some basic concepts used in this program.

Class.forName

              
This causes the driver class to dynamically load at  runtime and it causes the driver to get initialized and returns the class object associated with it. If the driver used is correct then the static intializer of the class can be invoked to get the connection. Rest stuffs are self explanatory.
import java.sql.*;
public class UserLogin {
public static void main(String[] args) {
try {
// Load MS accces driver class
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 
// C:\\databaseFileName.accdb" - location of your database 
String url = "jdbc:odbc:Driver={Microsoft Access Driver 
(*.mdb, *.accdb)};DBQ=" + "C:\\bank.accdb";
 
// specify url, username, pasword - make sure these are valid 
Connection conn = DriverManager.getConnection(url, "username", "password");
 
System.out.println("Connection Succesfull");
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
 
}}}


No comments:

Post a Comment