JSP combat: Create a database table in JBuilder2005

Source: Internet
Author: User
Tags insert log connect resource string format client oracle database
js| Create | data | database

1. In Oracle's SQL Plus tool, log on to the database as a user with DBA authority.

system/manger@to_128

The to_128 of the @ is the connection string name of the database, which needs to be changed according to the circumstances, and the @ and the connection string can be omitted if the database is local.

2. Create the Jbuser user and specify the password as ABC.

Sql> create user Jbuser identified by ABC;

3. Assign Connect and resource role permissions to Jbuser users.

Sql> Grant Connect, resource to Jbuser;

4. Log on to the database with Jbuser

Sql> Connect jbuser/abc@to_128;

5. Create user tables and sequences, and run the following SQL code in the sql> command.

Code Listing 1 creates the code for the table and the sequence:

       
        
         
        1.--Create User table 2. CREATE Table T_user (3. USER_ID CHAR (6) not null,4. User_name VARCHAR2 (60), 5. PASSWORD VARCHAR2 (20), 6. Constraint Pk_t_user primary KEY (USER_ID) 7. ); 8. --Create login log table 9. CREATE TABLE T_login_log (10. ID CHAR (a) not null,11. USER_ID CHAR (6) not null,12. Dt_login CHAR () not null,13. Dt_lonout CHAR (14), 14. Constraint Pk_t_login_log primary KEY (ID) 15. ); 16.17. --Creates an index that is used to generate the primary key 18 of the T_login_log table. Create sequence SEQ_LOGIN_LOG_ID19. Increment by 120. MaxValue 99999999999921. MinValue 100000000000;
       
        

6. Insert 3 historical Figures in the T_user user table as the initial user, run the following SQL code in the sql> command.

Code Listing 2 inserts 3 records into the T_user table

       
        
         
        1. Insert into T_user (User_id,user_name,password) VALUES (' 100000 ', ' Jiang teeth ', ' 123456 '), 2. Insert into T_user (User_id,user_name,password) VALUES (' 100001 ', ' Baoshu tooth ', ' 123456 '); 3. Insert into T_user (User_id,user_name,password) VALUES (' 100002 ', ' vertical teeth ', ' 123456 '); 4. Commit
       
        
  

Create Engineering and Web modules

After you create the database, open JBuilder and create the engineering and Web modules.

1. File->new Project ... Create a project called bookstore.

2. File->new...->web-> Double-click the Web Module (WAR) icon to create a web model with the name Webmodule. Choose Tomcat 5.0 as the Web application server.

Write Get database Connection class

You have to access the database through a data connection, and you need to get a database connection in multiple parts of the module, so we write a class that gets the data connection to increase the reuse rate of the code.

When writing a class to get a data connection, you must first add the Oracle JDBC Drive class bundle Classes12.jar to the Engineering Extensions Class library (Classes12.jar is located in the/Jdbc/lib directory), and we place Classes12.jar on the < engineering directory under >/orajdbclib. Use the following steps to introduce Classes12.jar in the engineering Extension class library:

Project->properties...->paths Settings page-> switch to required libraries-> Click Add...-> in the pop-up add to Project Classpath dialog box to switch to Archives tab, select the Engineering directory under the < Engineering directory >/orajdbclib/classes12.jar.

After introducing Oracle's JDBC Drive class pack Classes12.jar into the Engineering Extensions class library, create the DbConnection class in the project, and the code looks like this:

Code Listing 3 Dbconnection.java

       
        
         
        1. Package bookstore;2.3. Import java.sql.*;4. Import java.util.properties;5.6.  public class DbConnection {7.  Gets the database connection Class 8.  public static Connection getconnection () throws SQLException {9.   try {10. Class.forName ("Oracle.jdbc.driver.OracleDriver");   One.} catch (ClassNotFoundException ex) {12.   Ex.printstacktrace (); 13.  Return null;14.  }15.  Properties Sysprops = new properties (); 16.  Sysprops.put ("User", "Jbuser"); 17.  Sysprops.put ("Password", "abc"); 18.   Return Drivermanager.getconnection (19.  "Jdbc:oracle:thin:@192.168.0.128:1521:ora9i", sysprops); 20. }21. }
       
        
  

This class provides only a static method Getconnection (), with JBUSER/ABC to obtain a data connection located in 192.168.0.128,sid for ora9i.

There are two key points to getting a database connection:

1. Specify Database Drive class

As the 10th line of code shows, Oracle's JDBC Drive class name is: Oracle.jdbc.driver.OracleDriver, different databases have their own JDBC database drives, if you use other databases, please check the relevant information.

2, the specified database URL connection string

In line 19th, we specify a database URL connection string, different database URL connection string format is not the same, for Oracle database, database URL connection string contains 4 parts:

Jdbc:oracle:thin: Specifies the type of JDBC drive, where thin client drives are specified and are most commonly used without installing additional components on the connection client.

@192.168.0.128: The IP of the machine where the database is located, or the machine name can be used.

• 1521: The port where the database listener is located, typically Oracle defaults to port 1521.

ora9i: Database sid name.



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.