Update and query database instances using JDBC

Source: Internet
Author: User

Use JDBC to update and query database instances:

 
  1. PackageOrg. Oracle. insert;
  2. ImportJava. SQL. Connection;
  3. ImportJava. SQL. DriverManager;
  4. ImportJava. SQL. ResultSet;
  5. ImportJava. SQL. Statement;
  6. Public ClassInsertDeom {
  7. Public Static FinalString DBDRIVER ="Oracle. jdbc. driver. OracleDriver";
  8. Public Static FinalString DBURL ="Jdbc: oracle: thin: @ 127.0.0.1: 1521: ORCL";
  9. Public Static FinalString DBUSER ="Scott";
  10. Public Static FinalString DBPASS ="Tiger";
  11. Public Static VoidMain (String [] args)ThrowsException {
  12. Connection conn =Null;
  13. Statement stmt =Null;
  14. Class. forName (DBDRIVER );
  15. Conn = DriverManager. getConnection (DBURL, DBUSER, DBPASS );
  16. Stmt = conn. createStatement ();
  17. String SQL ="Insert into person (name, age, birthday, salary) values ('wang wu', 22, TO_Date ('2017-2-22 ', 'yyyy-mm-dd'), 1986)";
  18. Stmt.exe cuteUpdate (SQL );
  19. Stmt. close ();
  20. Conn. close ();
  21. }
  22. }

 

 
  1. PackageOrg. oracle. select;
  2. ImportJava. SQL. Connection;
  3. ImportJava. SQL. DriverManager;
  4. ImportJava. SQL. ResultSet;
  5. ImportJava. SQL. Statement;
  6. Public ClassResultDeom {
  7. Public Static FinalString DBDRIVER ="Oracle. jdbc. driver. OracleDriver";
  8. Public Static FinalString DBURL ="Jdbc: oracle: thin: @ 127.0.0.1: 1521: ORCL";
  9. Public Static FinalString DBUSER ="Scott";
  10. Public Static FinalString DBPASS ="Tiger";
  11. Public Static VoidMain (String [] args)ThrowsException {
  12. Connection conn =Null;
  13. Statement stmt =Null;
  14. ResultSet rs =Null;
  15. Class. forName (DBDRIVER );
  16. Conn = DriverManager. getConnection (DBURL, DBUSER, DBPASS );
  17. Stmt = conn. createStatement ();
  18. // Stmt.exe cuteUpdate ("insert into person (name, age, birthday, salary) values ('lily', 22, TO_Date ('2017-2-22 ', 'yyyy-mm-dd'), 4000 )");
  19. Rs = stmt.exe cuteQuery ("Select name, age, birthday, salary from person");
  20. While(Rs. next ()){
  21. System. out. print ("Name :"+ Rs. getString ("Name") +".");
  22. System. out. print ("Age :"+ Rs. getInt ("Age") +".");
  23. System. out. print ("Birthday :"+ Rs. getDate ("Birthday") +".");
  24. System. out. println ("Salary :"+ Rs. getFloat ("Salary") +".");
  25. }
  26. Rs. close ();
  27. Stmt. close ();
  28. Conn. close ();
  29. }
  30. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.