


Configuring hibernate2.1.2 connection mysql_PHP tutorial in resin3.0
Configure hibernate2.1.2 to connect mysql in resin3.0
Configure hibernate2.1.2 to connect to mysql in resin3.0Author: hamal
Agreement:
resin3 represents the installation root directory of resin3.0
hibernate2 represents the installation root directory of hibernate2.1.2
1. Establish our web application root under resin3, such as resin3mydomain directory (the directory name of mydomain can be chosen arbitrarily, and can be configured in the configuration file later)
2. Create the resin3mydomainWEB-INFclasses directory and the resin3mydomainWEB-INFlib directory in the resin3mydomain directory.
These two directories correspond to the class loader search path of this web application context (resin3mydomainWEB-INFlib for jars, resin3mydomainWEB-INFclasses for class files). We call these two paths the application library class path (used to store the jar class library related to this application) and the context class path (used to store the class file and xml configuration file of this application).
There is also a path called the resin3lib directory, which we call the global library class path (the related jar class library stored on the resin3 server and shared by all web applications on the server).
3. This example uses the mysql database, so we put the mysql jdbc driver jar package (mm.mysql-2.0.4-bin.jar) into the resin3lib directory. After that, the driver will be used by all web applications.
4. Copy the hibernate2 hibernate2.jar file to the resin3mydomainWEB-INFlib directory, and then copy the necessary jar files in the hibernate2lib directory to the resin3mydomainWEB-INFlib directory. If you are not sure which packages you need, you can refer to the hibernate2libREADME.txt file, or, to be simpler, we copy all the jar files in the hibernate2lib directory to the resin3mydomainWEB-INFlib directory.
5. Now we start to configure resin’s jdbc database connection pool. Modify resin3confresin.conf file
a) Search for webapps and replace webapps with our customized application directory mydomain.
b) Find the
c) Modify the
& Lt; url & gt; jdbc: mysql: //192.162.125.3: 3306/mysql & lt;/url & gt;
In this way, the configuration of resin's jdbc connection pool is completed.
6. Copy the hibernate.properties, log4j.properties, and oscache.properties files in the hibernate2src directory to the resin3 mydomainWEB-INFclasses directory.
7. Since we are using the mysql database, modify the configuration of the mysql part in the hibernate.properties file and comment out the original default HypersonicSQL configuration. Comment configuration is to add the # symbol before the statement. Such as:
#hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
The following is a typical mysql configuration:
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class org.gjt.mm.mysql.Driver
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://192.162.125.3:3306/mydb
hibernate.connection.username root
hibernate.connection.password 12345678
What we need to modify is the following 3 lines:
url refers to the jdbc connection descriptor, the format is jdbc:mysql://database IP:port number/database name
username refers to the username used to log in to the database
passwordThis user’s password
8. Bind the database connection pools of hibernate and resin. Create a hibernate.cfg.xml file in the directory with the following content
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
& Lt; Property name = "Connection.datasource" & GT; Java: Comp/ENV/JDBC/MySQL & LT;/Property & GT;
& Lt; Property name = "dialect" & gt; net.sf.hibernate.dialect.mqldialect & lt;/propro & gt;
The
It declares that User.hbm.xml is a Hibernate XML mapping file corresponding to the persistence class User. This file contains metadata that maps POJO classes to a database table (or multiple database tables). We'll come back to this document later. Let's write this POJO class first and then look at the mapping metadata that declares it.
9. Create a user table in mysql. The format of the table is as follows:
User_id Password Nick_name E_mail
1 6666 hamal hamal@sohu.com
2 6666 vampire vampire@sina.com
3 6666 ande ande@yahoo.com
Create three new java files in the resin3 mydomainWEB-INFclasses directory: Test.java, HibernateUtil.java, and User.java.
The source code of HibernateUtil.java is as follows: This class is an auxiliary class used to obtain a static SessionFactory. SessionFactory is responsible for a database and only corresponds to one XML configuration file (hibernate.cfg.xml).
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
}
}
Public static final ThreadLocal session = new ThreadLocal();
Public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
//Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
Public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
Session.set(null);
if (s != null)
s.close();
}
}
The source code of User.java is as follows: Hibernate turns ordinary Java objects (Plain Old Java Objects, POJOs, sometimes also called Plain Ordinary Java Objects) into persistent classes. A POJO is much like a JavaBean. Properties are accessed through getter and setter methods, hiding the internal implementation details from the outside.
public class User {
private Integer id;
private String nick;
private String password ;
private String email;
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Test.java 源代码如下:
import javax.naming.*;
import net.sf.hibernate.*;
import java.util.*;
public class Test{
void Test(){
}
public static void insert(){
try{
Session hSession = HibernateUtil.currentSession();
Transaction tx= hSession.beginTransaction();
User newp = new User();
Integer id = new Integer("4");
newp.setId(id);
newp.setNick("love");
newp.setPassword("123");
newp.setEmail("test@sohu.com");
hSession.save(newp);
tx.commit();
HibernateUtil.closeSession();
}catch(Exception e){
e.printStackTrace();
}
}
}
10. 编写Hibernate XML映射文件
在resin3 mydomainWEB-INFclasses目录下新建User.hbm.xml文件,文件内容如下:
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
Simple description:
The
The
11. Interface test:
We create a new test.jsp file in the resin3mydomain directory as follows:
<%@ page contentType="text/html; charset=gb2312" %>
This is a test!
<%Test.insert();%>
12. Test
Okay, now that all the preparations have been done, let’s start testing.
We start the rensin server, the startup file is resin3binhttpd.exe, double-click the file.
The contents of the user table in mysql we saw before the test are as follows.
User_id Password Nick_name E_mail
1 6666 hamal hamal@sohu.com
2 6666 vampire vampire@sina.com
3 6666 ande ande@yahoo.com
Now we open IE and enter http://localhost:8080/test.jsp
in the address bar
When This is a test! is displayed normally on the interface, we then check the database content as follows:
User_id Password Nick_name E_mail
1 6666 hamal hamal@sohu.com
2 6666 vampire vampire@sina.com
3 6666 ande ande@yahoo.com
4 123 love test@sohu.com
Congratulations, you have completed this example!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

Copying a table in MySQL requires creating new tables, inserting data, setting foreign keys, copying indexes, triggers, stored procedures, and functions. The specific steps include: creating a new table with the same structure. Insert data from the original table into a new table. Set the same foreign key constraint (if the original table has one). Create the same index. Create the same trigger (if the original table has one). Create the same stored procedure or function (if the original table is used).

Navicat for MariaDB cannot view the database password directly because the password is stored in encrypted form. To ensure the database security, there are three ways to reset your password: reset your password through Navicat and set a complex password. View the configuration file (not recommended, high risk). Use system command line tools (not recommended, you need to be proficient in command line tools).

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.
