02. Example of applications for lomboz and JDBC processing DDL statements, 02. lombozddl

Source: Internet
Author: User

02. Example of applications for lomboz and JDBC processing DDL statements, 02. lombozddl
Reprinted please indicate the source: http://blog.csdn.net/u012637501
1. lomboz development toolsLomboz is a major open-source plug-in (open-source plug-in) of Eclipse. The Lomboz plug-in enables Java developers to better use Eclipse to create, debug and deploy a Java application server 100% based on J2EE. The Lomboz plug-in enables Eclipse to integrate multiple J2EE elements, Web application development, and the most popular application server vehicles. There is no problem with replacing myeclipse with it. Here we only need to develop JDBC database applications.1. Download and installLomboz official website, http://lomboz.ow2.org/downloads.php. You can download lomboz from the above. The current version is not provided based on eclipse 3.4, So we download version 3.3. When downloading, only eclipse + lomboz versions with all-in-on are available. With prequest (install the plug-in to the pure version of Eclipse), it refers to other software packages that only lomboz + says to be dependent on, and the other is only lomboz. Here, we recommend that you use the all-in-one version directly to avoid configuration troubles. Version: linux: strongswan.2. Create a JDBC Application ProjectUsing lomboz to develop JDBC database applications is actually developing a common Java application (1) adding a MySQL database. jar package (driver package name) to the project, right-click the project name, select "mysql-connector .... -bin. jar "(2 create a Java Project: File-> New-> Java Project (3) create a JDBC application (multiple) in the Java Project ), run the project name (or a Java source file)-> right-click-> run as-> run on Java Application. note: To perform one-step debugging, select debug as-> run on Java Application.
Ii. database programming practices: JDBC processing DDL statementsDDL (Data manipulation language) Statements: Data Definition language. These statements define different Data objects, such as Data segments, databases, tables, columns, and indexes, common statement keywords include create, drop, select, and alter.1. Install MySQL databaseFirst, we need to install the MySQL database on the host (username is root, password is 111111), and create a database for JDBC application access create database jdbc_test_db (id tinyint primary key auto_increment, name varchar (10) not null default '', age tinyint not null default 0, score smallint not null default 0) charset UTF-8; insert several records as follows:
2. Create a Java Application project and add the database driver. jar package

  1. ImportJava. SQL .*;
  2. /* MySQL Database Programming
  3. * Instance (1): JDBC processes DLL statements */
  4. Public ClassTestJDBC_1 {
  5. Public Static VoidMain (String [] args ){
  6. // 0. Database URL, database account name, and password
  7. String url = "jdbc: mysql: // localhost/jdbc_test_db ";
  8. String DBusername = "root ";
  9. String DBpassword = "111111 ";
  10. // 1. Load the database driver to the Java Virtual Machine
  11. Try{
  12. Class. forName ("com. mysql. jdbc. Driver"); // The Driver is a MySQL Driver Class.
  13. }Catch(ClassNotFoundException e)
  14. {
  15. System. out. println ("the database driver class cannot be found. Failed to load the driver! ");
  16. E. printStackTrace (); // Save the exception to log
  17. }
  18. // 2. Create the Connection object conn to connect to the MySQL database.
  19. Connection conn =Null;
  20. Statement stmt =Null;
  21. ResultSet rs =Null;
  22. Try{
  23. Conn = DriverManager. getConnection (url, DBusername, DBpassword );
  24. // 3. Obtain the Statement object that can execute SQL statements.
  25. Stmt = conn. createStatement ();
  26. // 4. Execute the SQL statement and obtain the result set (the query result set contains multiple rows)
  27. Rs1_stmt.exe cuteQuery ("select * from test ");
  28. // 5. traverse all rows in the result set to obtain the specified data
  29. While(Rs. next ())
  30. {
  31. IntId = rs. getInt (1); // obtain the first column of all records
  32. System. out. print (id );
  33. String name = rs. getString (2); // obtain the second column of all records
  34. System. out. print (name );
  35. IntAge = rs. getInt (3); // obtain the third column of all records
  36. System. out. print (age );
  37. IntScore = rs. getInt (4); // obtain the fourth column of all records
  38. System. out. print (score );
  39. }
  40. }Catch(SQLException se)
  41. {
  42. System. out. println ("failed to connect to the Database ");
  43. Se. printStackTrace ();
  44. }
  45. // 6. Close all used JDBC objects and release JDBC Resources
  46. If(Rs! =Null) // Close the record set
  47. {
  48. Try{
  49. Rs. close ();
  50. }Catch(SQLException e ){
  51. E. printStackTrace ();
  52. }
  53. }
  54. If(Stmt! =Null) // Close the Declaration
  55. {
  56. Try{
  57. Stmt. close ();
  58. }Catch(SQLException e ){
  59. E. printStackTrace ();
  60. }
  61. }
  62. If(Conn! =Null) // Close the database connection
  63. {
  64. Try{
  65. Conn. close ();
  66. }Catch(SQLException e ){
  67. E. printStackTrace ();
  68. }
  69. }
  70. }
  71. }
3. Running result
Analysis: to determine whether a JDBC application is connected to a MySQL database, we only need to load the database driver and create a database connection. If no exception occurs during the operation, the database is connected successfully. Otherwise, check whether the database URL ("jdbc: mysql: // localhost: 3306/jdbc_test_db"), Database User and password, database name, and table name are correct. Reference http://blog.sina.com.cn/s/blog_495325a50100c1k8.html

Related Article

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.