JDBC Connection SQL Server code template

Source: Internet
Author: User

* JDBC Connection SQL Server database code template
* Connection: Connect the database and assume the task of transmitting data;
* Statement: Execute SQL statement;
* ResultSet: Saves the results of the query resulting from statement execution.

*
* Class.forName (JDBC driver Class);

* Connection dbconn=drivermanager.getconnection (JDBC URL, database user name, password);

Database information:

The code is as follows:

Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classJdbc_conn_sqlserver { Public Static voidMain (string[] args)throwssqlexception,classnotfoundexception {/********************************************************** * JDBC Connection SQL Server database code template *         * Connection: Connect the database and assume the task of transmitting data; * Statement: Execute SQL statement; * ResultSet: Save query results resulting from Statement execution.         * * Class.forName (JDBC driver Class);         * Connection dbconn = drivermanager.getconnection (JDBC URL, database user name, password); *          **********************************************************/        /********************************************************** * Database login name: SA * Database login password: SA * database name: TE      St * Table name: t_users * Field: * ID int * username nvarchar (50) * Password nvarchar () **/                //1. Registration DriverClass.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); //2. Get a database connectionConnection dbconn = drivermanager.getconnection ("jdbc:sqlserver://localhost:1433; Databasename=test "," sa "," sa "); //2. Get Statement ObjectStatement st =dbconn.createstatement (); //3. Execute SQL statementsResultSet rs = st.executequery ("SELECT * from T_users"); //4. Processing Results         while(Rs.next ()) {//Read and Save database query results            intx = Rs.getint ("ID"); String S1= Rs.getstring ("username"); String S2= rs.getstring ("Password"); //Print ResultsSystem.out.println ("id=" +x); System.out.println ("Username=" +S1); System.out.println ("Password=" +S2); }                //Freeing ResourcesRs.close ();        St.close ();    Dbconn.close (); }}

Program run:

JDBC Connection SQL Server code template

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.