Java and database connection methods (ii)

Source: Internet
Author: User
Data | database | Database connection Now the second method is connected by a corresponding driver provided by the vendor.

This is done directly by using a driver created with a dedicated network protocol provided by the database vendor, which translates the JDBC API call directly into a direct network call. This kind of call mode general performance is better, and also is the simplest method in the practical. Because it is necessary to install additional libraries or middleware. Almost all database vendors provide such a database for their databases that provide this JDBC driver, or they can be obtained from third-party vendors.

From the URL http://industry.Java.sun.com/products/jdbc/drivers/you can see a list of all the useful drivers. The result is as shown in the figure:

Application---JDBC API---driver---data source

First of all, install JDBC driver, recommend SP2 version, can download from Microsoft website
Http://www.microsoft.com/downloads/details.aspx? familyid=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&displaylang=en Download the bottom of the SETUP.EXE

This driver should match the SQL SERVER2000 SP3a, the corresponding download URL is
http://www.microsoft.com/china/sql/downloads/sp3.asp Download Chs_sql2ksp3.exe

If you compile directly with the Java SDK, you need to set the environment variables to set the Lib three files in the installed JDBC driver as environment variables:
Classpath
D:\program Files\Microsoft SQL Server\jdbc\lib\msbase.jar;
D:\program Files\Microsoft SQL Server\jdbc\lib\mssqlserver.jar;
D:\program Files\Microsoft SQL Server\jdbc\lib\msutil.jar;

Installation can use Microsoft's driver to connect to the database, the corresponding code is basically the same as before:

Import java.sql.*;
Import java.io.*;
public class DbColumn {

public static void Main (string[] args) {
Connection Con=null;
Statement Sm=null;
String Command=null;
ResultSet Rs=null;
String Tablename=null;
String Cname=null;
String Result=null;
BufferedReader input=new BufferedReader (New InputStreamReader (system.in));
Try
{
Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver");
SYSTEM.OUT.PRINTLN ("Driver Loaded");
SQL Server login must be logged in using SQL Server password authentication method
Con=drivermanager.getconnection ("jdbc:microsoft:sqlserver://servername:1433", "USER", "PASSWORD");
Con.setcatalog ("goodssupply");
System.out.println ("OK, connect to database successfully");
}catch (Exception ex) {
Ex.printstacktrace ();
}
Try
{
Sm=con.createstatement ();
SYSTEM.OUT.PRINTLN ("Input table name");
Tablename=input.readline ();
while (true) {
System.out.println ("Enter column name (end of NULL program):");
Cname=input.readline ();
if (Cname.equalsignorecase (""))
Break
command= "Select" +cname+ "from" +tablename;
Rs=sm.executequery (command);
if (!rs.next ())
SYSTEM.OUT.PRINTLN ("Wrong table name or column name entered incorrectly");
else {
SYSTEM.OUT.PRINTLN ("Query result is:");
Todo
{
Result=rs.getstring (CName);
Result=new String (result.getbytes ("iso-8859-1"), "GB2312");
SYSTEM.OUT.PRINTLN (result);
}while (Rs.next ());
}
}
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
}



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.