Saturday, May 26, 2012

Creating a Table and Inserting values in table (JAVA)




import java.sql.*;
import java.io.*;
class CreateTable
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:coll");
                                   Statement stmt=conn.createStatement();
      stmt.executeUpdate("create table student1(rno INTEGER primary key,name VARCHAR,per FLOAT)");
System.out.println("Table created");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the roll number");
int r = Integer.parseInt(br.readLine());
System.out.println("Enter name");
String name = br.readLine();
System.out.println("Enter per ");
double per=Double.parseDouble(br.readLine());
stmt.executeUpdate("insert into student1 values("+r+",' "+name+" ',"+per+")");
System.out.println("Record Inserted..");
stmt.close();
conn.close();
}
catch(Exception e){System.out.println(e.toString());}
}
}


No comments:

Post a Comment