Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.*;
- public class JDBC1_2
- {
- static final String JDBC_DRIVER="sun.jdbc.odbc.JdbcOdbcDriver";
- static final String DB_URL="jdbc:odbc:jdbc";
- static final String USER="system";
- static final String PASS="admin";
- public static void main(String[]args)
- {
- Connection conn=null;
- Statement stmt=null;
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- conn=DriverManager.getConnection(DB_URL,USER,PASS);
- stmt=conn.createStatement();
- String csql="Create Table database17 (id INTEGER not NULL,Name VARCHAR(30),Age INTEGER);";
- stmt.executeUpdate(csql);
- System.out.println("Table Created");
- String insql="INSERT INTO database17 VALUES(1,'Bhavesh',20);";
- stmt.executeUpdate(insql);
- System.out.println("One record inserted");
- insql="INSERT INTO database17 VALUES(2,'Mahesh',20);";
- stmt.executeUpdate(insql);
- System.out.println("Second record inserted");
- insql="INSERT INTO database17 VALUES(3,'Shubham',20);";
- stmt.executeUpdate(insql);
- System.out.println("Third record inserted");
- insql="INSERT INTO database17 VALUES(4,'Ajit',20);";
- stmt.executeUpdate(insql);
- System.out.println("Fourth record inserted");
- System.out.println("----------------------");
- System.out.println("ID NAME AGE");
- System.out.println("----------------------");
- String sql1="select * from database17;";
- ResultSet result1 = stmt.executeQuery(sql1);
- while(result1.next())
- {
- System.out.println(result1.getInt(1)+ " " +result1.getString(2)+ " " +result1.getInt(3));
- }
- String upsql="UPDATE database17 SET NAME='Maddy' WHERE id=2;";
- stmt.executeUpdate(upsql);
- System.out.println("Record Updated");
- System.out.println("Records After Updated");
- System.out.println("----------------------");
- System.out.println("ID NAME AGE");
- System.out.println("----------------------");
- String sql="select * from database17;";
- ResultSet result = stmt.executeQuery(sql);
- while(result.next())
- {
- System.out.println(result.getInt(1)+ " " +result.getString(2)+ " " +result.getInt(3));
- }
- String delsql="drop table database17;";
- stmt.executeUpdate(delsql);
- System.out.println("Table deleted");
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if(stmt!=null)
- conn.close();
- }
- catch(SQLException se)
- {
- se.printStackTrace();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment