Java Database Connection:
1. Oracle database:
String driver="oracle.jdbc.driver.OracleDriver";
String username = "tiger";
String password = "1234";
String oracleURL = "jdbc:oracle:thin:@localhost:1521:school_db";//database name school_db,port number=1521
try{
Class.forName(driver);
Connection c=DriverManager.getConnection(oracleURL,username,password);
return c;
}
catch(Exception e)
{
System.out.println("error in connection"+e);
return null;
}
2. Mysql database:
String driver="com.mysql.jdbc.Driver";
String username = "tiger";
String password = "1234";
String oracleURL = "jdbc:mysql://localhost:3306:school_db";//database name school_db,
port number by default
=3306
try{
Class.forName(driver);
Connection c=DriverManager.getConnection(oracleURL,username,password);
return c;
}
catch(Exception e)
{
System.out.println("error in connection"+e);
return null;
}
3. Mariadb database:
String driver="org.mariadb.jdbc.Driver";
String username = "root";
String password = "1234";
String oracleURL = "jdbc:mysql://localhost:3306/school_db";
//database name school_db,port number by default=3306
try{
Class.forName(driver);
Connection c=DriverManager.getConnection(oracleURL,username,password);
return c;
}
catch(Exception e)
{
System.out.println("error in connection"+e);
return null;
}
NOte- To connect java application with the [oracle,mysql,mariadb]database,[oracle,mysql,mariadb] connector.jar file is required to be loaded.
No comments:
Post a Comment