02. Examples of applications for lomboz and JDBC processing DDL statements

Source: Internet
Author: User

02. Examples of applications for lomboz and JDBC processing DDL statements
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. After jdk is installed, decompress the package directly.

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 and execute the Project name (or a Java source File) -> right-click and choose 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

Import java. SQL. *;/* MySQL database programming * instance (1): JDBC processing DLL statement */public class TestJDBC_1 {public static void main (String [] args) {// 0. database URL, database account name, and password String url = "jdbc: mysql: // localhost/jdbc_test_db"; String DBusername = "root"; String DBpassword = "111111 "; // 1. load the database driver to the Java Virtual Machine try {Class. forName ("com. mysql. jdbc. driver "); // The Driver is a MySQL Driver Class} catch (ClassNotFoundException e) {System. out. println ("unable to find the database Driver Class, failed to load the driver! "); E. printStackTrace (); // Save the exception to log} // 2. create a Connection object conn, indicating to connect to the MySQL database Connection conn = null; Statement stmt = null; ResultSet rs = null; try {conn = DriverManager. getConnection (url, DBusername, DBpassword); // 3. obtain the Statement object stmt = conn that can execute SQL statements. createStatement (); // 4.execute the SQL statement and obtain the result set. (The result set for the query is multiple rows. rs1_stmt.exe cuteQuery ("select * from test"); // 5. traverse all rows in the result set to obtain the specified data while (rs. next () {int id = rs. getInt (1); // obtain the first of all records Column System. out. print (id); String name = rs. getString (2); // obtain the second column of all records, System. out. print (name); int age = rs. getInt (3); // obtain the third column of all records, System. out. print (age); int score = rs. getInt (4); // obtain the fourth column of all records, System. out. print (score) ;}} catch (SQLException se) {System. out. println ("failed to connect to the Database"); se. printStackTrace ();} // 6. close all used JDBC objects and release JDBC resources if (rs! = Null) // close the record set {try {rs. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (stmt! = Null) // close the Declaration {try {stmt. close () ;}catch (SQLException e) {e. printStackTrace () ;}} if (conn! = Null) // close the database connection {try {conn. close () ;}catch (SQLException e) {e. printStackTrace ();}}}}

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.

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.