package dbtest;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import oracle.jdbc.*;
public class Dbtest
{
public static void main(String[] args)
{
String tableCreateQuery = "create table MYTEMPTABLE(name varchar2(20), course varchar2(20))";
String inserRecordQuery= "insert into MYTEMPTABLE values('hello','world')";
Connection connection = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("Stack Trace 1");
e.printStackTrace();
}
System.out.println("Passed 1: Found oracle.jdbc.driver.OracleDriver");
try
{
connection = DriverManager.getConnection("jdbc:oracle:thin:@myhost:mySID", "user", "pass");
}
catch (SQLException e)
{
System.out.println("Stack Trace 2");
e.printStackTrace();
}
System.out.println("Passed 2: Successful connection to db");
if (connection != null)
{
try
{
Statement stmt = connection.createStatement();
stmt.execute(tableCreateQuery);
System.out.println("Table Created Successfully");
stmt.execute(inserRecordQuery);
System.out.println("Record Inserted Successfully");
}
catch (SQLException e)
{
e.printStackTrace();
}
}
else
{
System.out.println("connection is NULL! You did not connect to db!");
}
}
}
No comments:
Post a Comment