server| Skills | data | database | database connection
Recently the company is ready to do a small project, DB with SQL SERVER 2000, development platform for JAVA2, development tools to use JBUILDER9, so I carefully analyzed in this case the database connection method. In fact, as we all know, Java and DB connection is divided into four types:
(1) Type1: Use JDBC-ODBC Bridge to implement DB connection, this method is suitable for use on the Windows platform, the disadvantage is not used on the applet, if you want to implement DB communication on the client, you must set up an ODBC data source.
(2) Type2: The function Native-api Bridge realizes the DB connection, this method needs to install the DB developer software on the client, is not advantageous to cross platform.
(3) Type3: Use Net-protocol to implement DB connection, this method requires a Third-party server.
(4) Type4: Use Native-protocol fully Java technology-enabled technology to achieve DB connectivity, do not need intermediate server, the entire process of communication with DB is implemented by the Java language.
From the above we are most commonly used is the first and fourth methods, in the first method, the key to establish JDBC-ODBC is to create an ODBC data source, write JDBC-ODBC database connection is a sentence: Jdbc:odbc:ODBCName. In the fourth approach, the key to establishing a JDBC connection is to write a URL, and the syntax format for writing the URL is jdbc:<protocol>:<databasename>.
The general method for establishing a DB connection is as follows:
(1) Register class, the method used is Class.forName (typestring).
(2) Declare the connection class connection.
(3) A connection is created by the getconnection (Url,user,password) method of the DriverManager class (this method is a static method). The writing of its URL is critical, and the syntax format is:
Jdbc:<protocol>:<databasename>protocol: Represents the connection mechanism that defines DB DatabaseName: name of DB
The specifics of the implementation, depending on the DBMS, are now implemented as an example of SQL SERVER 2000. This selection of the fourth method, mainly divided into the following steps:
1, install SQL Server 2000, and install JDBC for SQL Server 2000.
2, create a new project in JBuilder9, and set the required class package for the project properties. The steps are: tool->configure libraries...->new Open the New Class Library Wizard, a name in name, such as SQLSERVERJDBC, and then the JDBC for SQL SERVER 2000 the three. jar files in the Lib directory under the installation directory are added to this package to confirm the exit. Then add the new class package to the Project->project properties->path->required libraries.
3, write DB connection program, or in the form of JavaBean, here, just for 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, here is a thought, you can use JavaBean to create a common DB connection class, by modifying the DB name, URL, user name and password to establish a connection to different db. Here will not implement the specific code, and so on the day to come to realize it,:
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.