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
- ImportJava. SQL .*;
- /* MySQL Database Programming
- * Instance (1): JDBC processes DLL statements */
- Public ClassTestJDBC_1 {
- Public Static VoidMain (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 ("the database driver class cannot be found. Failed to load the driver! ");
- E. printStackTrace (); // Save the exception to log
- }
- // 2. Create the Connection object conn 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 that can execute SQL statements.
- Stmt = conn. createStatement ();
- // 4. Execute the SQL statement and obtain the result set (the query result set contains 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 ())
- {
- IntId = rs. getInt (1); // obtain the first column of all records
- System. out. print (id );
- String name = rs. getString (2); // obtain the second column of all records
- System. out. print (name );
- IntAge = rs. getInt (3); // obtain the third column of all records
- System. out. print (age );
- IntScore = 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. Reference http://blog.sina.com.cn/s/blog_495325a50100c1k8.html