import java.io.*;
import java.net.*;
import java.sql.*;
public class Porting
{
static final String DRIVER_NAME = "sun.jdbc.odbc.JdbcOdbcDriver";
static Connection conn = null,con1=null;
public static void main(String[] args)
{
try
{
Class.forName(DRIVER_NAME);
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnectio("jdbc:odbc:aaa");
con1=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee/user=null&password=null");
if(con1==null)
{
System.out.println("Connection not established");
}
else
{
System.out.println("Connection established");
}
PreparedStatement prp=null;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from tablename");
int i=1;
while (rs.next())
{
prp=conn.prepareStatement("insert into tablename values (?,?)");
prp.setString(1,rs.getString("uid"));
prp.setString(2,rs.getString("PWD"));
int x=prp.executeUpdate();
if (x!=0)
{
System.out.println(i+" record is inserted");
}
else
{
System.out.println("record is not inserted");
}
i++;
}
rs.close();
stmt.close();
}
catch (ClassNotFoundException cnfe)
{
System.err.println("ClassNotFoundException Was Thrown");
cnfe.printStackTrace();
}
catch (SQLException sqle)
{
System.err.println("SQLException Was Thrown");
sqle.printStackTrace();
}
finally
{
try
{
if(conn!=null)
{
conn.close();
}
if(con1!=null)
{
con1.close();
}
}
catch(Exception e)
}
}
}
Thursday, August 16, 2007
Porting Data From one Database to another Database using JDBC
Posted by ramesh at 10:24 AM 1 comments
Creating Tables in Java using JDBC
import java.sql.*;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.*;
import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
class CreateTableEx{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection"jdbc:mysql://localhost:3306/test/user=yes&password=null");
Statement stmt=con.createStatement();
stmt.executeUpdate("create table tablename(eno integer,ename varchar(20),esal float,eaddr varchar(30))");
stmt.executeUpdate("insert into table values(01,'ramesh',100,'banjarahills')");
stmt.executeUpdate("insert into table values(02,'ram',5000,'ameerpet')");
stmt.close();
stmt.close();
}
catch(SQLException e)
{
System.out.println(e);
}
catch(ClassNotFoundException e){
System.out.println(e);
}
}
}
Posted by ramesh at 9:58 AM 0 comments
sample program for adding two numbers in java
import java.lang.*;
class Sum
{
public Static voidmain(String args[]);
{ int x,y;
x=10;
y=20;
int z=x+y;
System.out.println("The sum of two numbers is "+z);
}
}
Posted by ramesh at 9:45 AM 2 comments