import java.sql.*;
class InsertValues
{
public static void main(String args[])
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:student","","");
Statement stmt=con.createStatement();
System.out.println("Connetion establish");
stmt.executeUpdate("create table Employeees (" + "ID INTEGER, " +"Name VARCHAR(30))");
System.out.println("Table Created");
stmt.executeUpdate("insert into Employees"+ "(ID,Name)"+ " values(1234, 'Shawn')");
stmt.executeUpdate("insert into Employees"+ "(ID,Name)"+ " values(2, 'Shawn')");
System.out.println("Row insert");
ResultSet message = stmt.executeQuery("select * from Employees ");
message.next();
System.out.println(message.getString(2));
stmt.executeUpdate("delete from Employees where id=2");
stmt.executeUpdate("update Employees set name='rose' where id=2");
System.out.println("Row is updated");
stmt.executeUpdate("delete from Employees where id=2");
stmt.close();
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
/*
For Inserting Data:
import java.sql.*;
class AccessDatabase{
public static void main(String[] args){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:student");
Statement st=con.createStatement();
String name="roseindia";
String address="delhi";
int i=st.executeUpdate("insert into user(name,address) values('"+name+"','"+address+"')");
System.out.println("Row is added");
}
catch(Exception e){
System.out.println(e);
}
}
}
For deleting data:
import java.sql.*;
class AccessDatabase{
public static void main(String[] args){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:student");
Statement st=con.createStatement();
String name="rose1";
String address="delhi";
int i=st.executeUpdate("delete from user where id=4");
System.out.println("Row is deleted");
}
catch(Exception e){
System.out.println(e);
//catch (Exception s){ System.out.println(" not executed!"); } } }
}
}
}
For updating data:
import java.sql.*;
class AccessDatabase{
public static void main(String[] args){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:student");
Statement st=con.createStatement();
String name="roseindia";
String address="delhi";
int i=st.executeUpdate("update user set name='rose', address='rohini' where id=5");
System.out.println("Row is updated");
}
catch(Exception e){
System.out.println(e);
}
}
}
For searching data:
import java.sql.*;
class AccessDatabase{
public static void main(String[] args){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:student");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from user where id=5");
while(rs.next()){
System.out.println(rs.getString("name")+" "+rs.getString("address"));
}
}
catch(Exception e){
System.out.println(e);
}
}
}
*/
0 comments:
Post a Comment