java to oracle database connection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ludablay
    New Member
    • Oct 2007
    • 4

    #1

    java to oracle database connection

    Hello everyone, i'm new here and i'm having some problem with connecting my tables in oracle and editing (update/delete) them in a java app i'm building. a short example would also help. i am using oracle 10g.
    thanks.
  • rahana
    New Member
    • Oct 2007
    • 12

    #2
    import java.sql.*;

    public class Stmt
    {
    public static void main(String args[])throws Exception
    {

    try{
    Class.forName(" oracle.jdbc.dri ver.OracleDrive r");
    Connection con=DriverManag er.getConnectio n("jdbc:oracle: thin:@localhost :1521:xe","scot t","tiger");
    Statement stmt=con.create Statement();
    String vsql="create table testtable(cone number,ctwo number)";
    stmt.executeUpd ate(vsql);
    System.out.prin tln("...Success ....");
    con.close();
    }
    catch(ClassNotF oundException e)
    {
    System.out.prin tln(e);
    System.out.prin tln("hello");
    }

    }
    }
    --------------------------------------------------------------------------------------------------------------------
    ur url might change depending up on ur username,passwo rd.data source name etc.

    u can use various statement like
    String vsql=insert into Student values("+vstudn o+",' "+vstudname +" ');

    executeUpdate must be used for executing non select statements.
    executeQuery must be used for executing select statements.
    example:
    Statement stmt=con.Create Statement();
    String vsql="select * from Student";
    ResultSet rs=stmt.execute Query(vsql);
    while(rs.next() )
    {
    System.out.prin tln("current row--->"+rs.getRow( ))
    }
    con.close();
    }
    }

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      @Rahana: so you managed to make a database connection? What did you change?
      I can't find any difference between your non-working version and this one.

      kind regards,

      Jos

      Comment

      Working...