JDBC Access database

Source: Internet
Author: User
Tags access database

I. Conditions of preparation
    1. External conditions
    • Create a table space first in the database
    • Add data to the table you created
    1. Code section
    • Import a database driver package (JAR)
    • Load Database Driver
    • Get database connection
    • Writing SQL statements
    • Pretreatment with Preparestatement
    • Set parameters, Parameter 1 represents the first parameter (starting at 1), parameter 2 represents the value of the parameter
    • Issue a SQL statement to the database for querying, ExecuteQuery (), and querying to the result set resultset
    • Traversing result Sets
    • Freeing Resources (connection,resultset,preparestatement)
    1. Benefits of Preparestatement
    • To achieve the purpose of preprocessing, improve efficiency (as we write code, each write a sentence will be compiled, inefficient, if the use of preparestatement preprocessing, so that if we write to the same SQL statement before, it can not be compiled, So that the efficiency can be increased)
  1. Source
  2. public static void Main (string[] args) {
  3. Declaration link
  4. Connection Connection=null;
  5. Pretreatment
  6. PreparedStatement Preparedstatement=null;
  7. Get the result set
  8. ResultSet Resultset=null;
  9. Load Driver
  10. try {
  11. Class.forName ("Com.mysql.jdbc.Driver");
  12. Get links
  13. Connection = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/jdbc", "root", "123456");
  14. Creating SQL statements
  15. String sql= "SELECT * from user where username=? ";
  16. Pretreatment
  17. PreparedStatement = connection.preparestatement (sql);
  18. Preparedstatement.setstring (1, "Harry");
  19. ResultSet = Preparedstatement.executequery ();
  20. while (Resultset.next ()) {
  21. System.out.println (resultset.getstring ("id") +resultset.getstring ("username"));
  22. }
  23. } catch (Exception e) {
  24. TODO auto-generated Catch block
  25. E.printstacktrace ();
  26. }finally{
  27. Freeing resources
  28. if (resultset!=null) {
  29. try {
  30. Resultset.close ();
  31. } catch (Exception E2) {
  32. Todo:handle exception
  33. }
  34. }if (connection!=null) {
  35. try {
  36. Connection.close ();
  37. } catch (Exception E2) {
  38. Todo:handle exception
  39. }
  40. }if (preparedstatement!=null) {
  41. try {
  42. Preparedstatement.close ();
  43. } catch (Exception E2) {
  44. Todo:handle exception
  45. }
  46. }
  47. }
  48. }

JDBC Access database

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.