How to use Jtds to drive a connection to a database and add data to a database by using hibernate in MyEclipse

Source: Internet
Author: User

Recently learned how to use Jtds Drive connection database in MyEclipse, and add data to the database method, although myeclipse in the connection with the database method, I also tried the other methods, if there is inappropriate, please advise, as follows:

The required driver package is: Jtds-1.2.jar

Create a Java project first, call Testjtds, and add hibernate support to your project

right mouse button on Testjtds, find Myeclipse-->add Hibernate capabilities at the bottom ... window appears after

Of course, direct next, and then to select the database

Continue next. Then you have to choose a package to place the generated file, and the new one will be OK. That's it

So here's the file that's generated.

After performing the above operation, open the Hibernate.cfg.xml file now and change the port of the connection database to the following
<property name= "Connection.url" >jdbc:jtds:sqlserver://127.0.0.1:1433</property>

Of course, don't forget to change the driver
<property name= "Connection.driver_class" >net.sourceforge.jtds.jdbc.Driver</property>

The connection to the database is basically over.

My database is using the SQL sever2008 Connection section may be slightly different

It is important to note that the URL address and driver class are the ports of the database and the respective driver packages.

Let's test if it works.


First set up a data table with two fields
Depid int primary Key increment is 1,
Depname nvarchar (20)

Then create the mapping file for the data table and the Pojo class


This requires that the Java class and the. hbm.xml file for the data table have been automatically generated.

Next write a test class, before too much wordy, directly on the code bar

Import org.hibernate.*;
Import org.hibernate.cfg.*;
Import javax.persistence.entity;//using the Entity with JPA
Import org.hibernate.annotations.entity;//where the problem is
public class Test {
public static void Insertdep ()
{

Configuration Configuration=new configuration (). Configure ();
Configuration.configure ("/hibernate.cfg.xml");

Sessionfactory sessionfactory=configuration.buildsessionfactory ();
Session session=sessionfactory.opensession ();
Transaction trans=session.begintransaction ();

Department Dep=new Department ();
Dep.setdepname ("sales department");
Session.save (DEP);

Trans.commit ();
Session.close ();

}
}

Then directly run the test class, query the database can, if there is a problem, please advise, thank you.

How to use Jtds to drive a connection to a database and add data to a database by using hibernate in MyEclipse

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.