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 Creating the code for tables and sequences
1. --创建用户表
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. --创建登录日志表
9. create table T_LOGIN_LOG (
10. ID CHAR(12) not null,
11. USER_ID CHAR(6) not null,
12. DT_LOGIN CHAR(14) not null,
13. DT_LONOUT CHAR(14),
14. constraint PK_T_LOGIN_LOG primary key (ID)
15. );
16.
17. --创建索引,用于生成T_LOGIN_LOG表的主键
18. create sequence SEQ_LOGIN_LOG_ID
19. increment by 1
20. maxvalue 999999999999
21. 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','姜子牙','123456');
2. insert into T_USER(USER_ID,USER_NAME,PASSWORD) values('100001','鲍叔牙','123456');
3. insert into T_USER(USER_ID,USER_NAME,PASSWORD) values('100002','竖牙','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 obtain 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 directory of the <oracle installation directory >/jdbc/lib). We put the Classes12.jar in the < Engineering catalogue >/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. //获取数据库连接类
8. public static Connection getConnection() throws SQLException {
9. try {
10. Class.forName("oracle.jdbc.driver.OracleDriver");
11. } 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.