Friday, October 31, 2014

Database connection program implemented by Java Language

import java.sql.*;
public class database1
{
Connection data;
Statement datarequest;
ResultSet result;
String c1,c2,p1;
boolean record;

database1()
{
String url="jdbc:odbc:student2014";

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
data=DriverManager.getConnection(url);
System.out.println("Successful connection");
}catch(ClassNotFoundException e)
{
System.out.println("Unable to load "+e);
System.exit(1);
}catch(SQLException e1)
{
System.out.println(e1);
if(data!=null)
{
try
{
data.close();
}catch(SQLException e2) {}
}
}
}
void show()
{
try
{
String query="SELECT * FROM STUDENT";
datarequest=data.createStatement();
result=datarequest.executeQuery(query);
record=result.next();
}catch(SQLException e1)
{
System.exit(0);
}
if(!record)
{
System.out.println("No record found");
System.exit(0);
}
try
{ System.out.println("........Student table........");
do
{
c1=result.getString(1);
c2=result.getString(2);
p1=c1+ "  "+ c2;
System.out.println(p1);
}while(result.next());
datarequest.close();
}catch(SQLException e2){}
}

public static void main(String args[])
{
final database1 data1=new database1();
data1.show();
System.exit(0);
}

}

No comments:

Post a Comment