JBuilder9 + SQL SERVER 2000 database connection skills

Source: Internet
Author: User
Tags odbc
Recently, the company is preparing to develop a small project. The database uses SQL server 2000, the development platform is Java2, and the development tool uses JBuilder9. So I carefully analyzed the database connection method in this case. As we all know, Java and DB connections are mainly divided into four types:
(1) Type1: use JDBC-ODBC Bridge to achieve DB connection, this method is suitable for use on WINDOWS platform, the disadvantage is that it cannot be used on APPLET, if you want to implement DB communication on the client, you must set the ODBC data source.
(2) Type2: Native-API bridge for DB connection. This method requires the database developer software to be installed on the client, which is not conducive to cross-platform access.
(3) Type3: use Net-protocol for DB connection. This method requires a third-party server.
(4) Type4: Native-protocol fully Java technology-enabled technology is used to achieve DB connections without intermediate servers. The whole process of communication with DB is implemented by Java.
From the above we can see that the most commonly used is the first and fourth method, in the first method, the key to the establishment of JDBC-ODBC is to create ODBC data source, write the database connection of JDBC-ODBC is a sentence: jdbc: odbc: ODBCName. In the fourth method, the key to establishing a JDBC connection is to write a URL. The URL syntax format is jdbc: <protocol >:< DatabaseName>.
Follow these steps to establish a DB connection:
(1) Register the Class by using Class. forName (TypeString ).
(2) declare the Connection class Connection.
(3) create a connection by using the getConnection (url, user, password) method of the DriverManager class (this method is static ). The URL is the key, and the syntax format is:
Jdbc: <protocol >:< DatabaseName> protocol: defines the database connection mechanism. DatabaseName: indicates the database name.
The specific implementation of SQL server is different from that of DBMS. SQL SERVER 2000 is used as an example. The fourth method is used here, which includes the following steps:
1. Install SQL server 2000 and install JDBC for SQL SERVER 2000.
2. Create a project in JBuilder9 and set the required class package for the project properties. The procedure is Tool-> Configure Libraries... -> new: open the new class library wizard and place a Name in Name, such as SQLServerJDBC. Then, run the JDBC for SQL SERVER 2000 command to install three in the lib directory. add the jar file to the package and exit. Then add the class package created above to project> project properties> path> required libraries.
3. Compile the Database Connection program or use a JavaBean format. Here, we only provide the example. The source file is as follows:
Import java. SQL. *; public class sqlsDemoTest {String connType = "com. microsoft. jdbc. sqlserver. SQLServerDriver "; String DBurl =" jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = demo "; String user =" sa "; String pass =" 123 "; connection conn; public Connection getConnection () {try {Class. forName (connType); Connection conn = DriverManager. getConnection (DBurl, user, pass); System. out. println ("DB Connection Success. ");} catch (Exception ex) {ex. printStackTrace ();} return conn;} public sqlsDemoTest () {conn = this. getConnection (); if (conn! = Null) try {conn. close ();} catch (Exception e) {e. printStackTrace () ;}} public static void main (String [] args) {new sqlsDemoTest ();}}
In addition, we also propose an idea to use JavaBean to create a common DB connection class, by modifying the database name, URL, user name, and password, you can establish connections to different databases. The specific code will not be implemented for the time being. Please try again when you have time ,:)

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.