Program to show Data Base Connectivity Using JAVA

In the course of modernizing a record keeping system, you encounter a flat file of data that was created long before the rise of the modern relational database. Rather than type all the data from the flat file into the DBMS, you may want to create a program that reads in the text file, inserting each row into a database table, which has been created to model the original flat file structure.

import java.sql.*;
import java.io.*;
class UserConn
{
public static void main(String args[]) throws SQLException,IOException
{
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
System.out.println("Please enter the following information :");
String user;
String password;
String database;
user = readEntry("Users :");
int slashindex = user.indexOf('/');
if(slashindex != -1)
{
password = user.substring (slashindex + 1);
user = user.substring (0,slashindex);
}
else
password = readEntry ("Password: ");
database = readEntry ("Database: ");
System.out.flush();
try
{
System.out.println(user);
System.out.println(password);
System.out.println(database);

Connection cn =DriverManager.getConnection ("jdbc:odbc:" + database,user,password);
System.out.println("Connected to the database");
Statement st = cn.createStatement();
st.executeUpdate("create table Product(Prodid number(3),ProdName varchar2(10),Price number(5,2),stock_on_hand number(4))");
System.out.println("Table Product Created");
Statement stat = cn.createStatement();
stat.executeUpdate("create table Transaction(CustId Number(3),ProId Number(3) ,TranId Number(3),Qty Number(2),TranDt Date)");
System.out.println("Table Transaction Created");
st.close();
cn.close();
}
catch (Exception ex)
{
System.out.println("The exception in creating the table is " + ex);
}
}
static String readEntry (String prompt)
{
try
{
StringBuffer tempo = new StringBuffer ();
System.out.print(prompt);
System.out.flush();
int c = System.in.read ();
while (c!= '\n' && c != -1)
{
tempo.append ((char)c);
c = System.in.read();
}
return tempo.toString ().trim();
}
catch (IOException ex)
{
return"Error";
}
}
}

s

No comments:

Google Search