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