SQL Server's application in Java

Source: Internet
Author: User
Tags odbc sql first row
Server|sqlserver
I. Application of JDBC-ODBC Bridge

1. Building databases and tables in SQL Server

2. Create a data source]

(1) Start-> Settings-> Control Panel-> admin tools->odbc Data Source

(2) Click the Add button under the User DSN to eject the Create Newdatasource dialog box

(3) Select Sqlserver-> Complete

(4) Specifies that name is your data source name, server is the IP address of the host computer where the SQL Server is located, and if it is local, it can be localhost,127.0.0.1.

(5) Select With...............by User, set username and password

(6) Select Change the default database to:

Select the appropriate database in the Drop-down list

(7) Test DataSource (test data source), failed to return third step reconfiguration.

3. Example

(1) Create the XS database in SQL Server and create the XS table in the XS database with the following fields:

Name
Sex
Age

Cjp
Male
22


(2) Configure the data source according to the 2nd step above, named XS

(3) Code:

Import java.sql.*;//SQL Package



public class XS {

public static void Main (string[] args) throws exception{

Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");//Load Driver

Connection con=drivermanager.getconnection ("Jdbc:odbc:xs");//Get data source and connect

Statement st=con.createstatement ();//Create statement



ResultSet result=st.executequery ("SELECT * from XS");/execute query, get data set in unit of behavior



while (Result.next ()) {//traverse each row

System.out.println (result.getstring (1));//Get the first column of the first row

System.out.println (result.getstring (2));//Get the second column of the first row

System.out.println (Result.getstring (3))//Get the third column of the first row

}



}

}



Results:

Cjp

Male

22







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.