//For Getting the information about the Database Using Java
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import com.lara.util.JDBCUtil;
public class DatasOfDatabaseUsingJava
{
public static void main(String[] args)
{
Connection con = null;
DatabaseMetaData dbmd = null;
try
{
con = JDBCUtil.getConnection();
dbmd = con.getMetaData();
System.out.println("Database Driver Name : "+dbmd.getDriverName());
System.out.println("Database Product Name : "+dbmd.getDatabaseProductName());
System.out.println("Database Major Version : "+dbmd.getDatabaseMajorVersion());
System.out.println("Database Minor Version : "+dbmd.getDatabaseMinorVersion());
System.out.println("Database Product Version : "+dbmd.getDatabaseProductVersion());
System.out.println("Database Driver Major Version : "+dbmd.getDriverMajorVersion());
System.out.println("Database Driver Minor Version : "+dbmd.getDriverMinorVersion());
System.out.println("Database Driver Version : "+dbmd.getDriverVersion());
System.out.println("Database JDBC Major Version : "+dbmd.getJDBCMajorVersion());
System.out.println("Database JDBC Minor Version : "+dbmd.getJDBCMinorVersion());
System.out.println("Database User Name : "+dbmd.getUserName());
System.out.println("Database URL : "+dbmd.getURL());
}
catch(SQLException ex)
{
ex.printStackTrace();
}
finally
{
JDBCUtil.closeJDBCResourcesForStatement(null, null, con);
}
}
}
JDBCUtil.java
import java.sql.*;
import java.util.*;
public class JDBCUtil
{
static
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
}
public static Connection getConnection() throws SQLException
{
return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "password");
}
public static closeJDBCResources(Statement stmt, ResultSet rs, Connection con)
{
if(stmt != null)
{
try
{
stmt.close();
stmt = null;
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
if(rs != null)
{
try
{
rs.close();
rs = null;
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
if(con != null)
{
try
{
con.close();
con = null;
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
}
}
No comments:
Post a Comment