JDBC Connection SQL Server database sample _java

Source: Internet
Author: User

1. Process:

1> registered Drive class: Class.forName ()

2> Connection Database:

Copy Code code as follows:

String url = "Jdbc:sqlserver://localhost:1433;databasename=databasename";
String uername = "Dbuser";
String password = "secret";
Connection conn = drivermanager.getconnection (Url,username,password);

3> Execute SQL statement:

Statement stat = conn.createstatement ();
String command = "UPDATE books";
Stat.executeupdate (command);
[/code]

2.java.sql.statement () (three methods of execution)

First: Execute (); void executes any SQL statement

The second type: Executeupdate (); int

Can be performed: INSERT UPDATE DELETE
CREATE Table;drop TABLE

Return: Number of rows affected
The third type: ExecuteQuery (); ResultSet
Execution: SELECT
Returns: A ResultSet object, one line iteration at a time
Cases:

Copy Code code as follows:

ResultSet rs = stat.executequery ("SELECT * FORM books");
while (Rs.next ()) {
Look in a row of the result set
}
Rs.getstring (1); Returns the value of the first column in the current row
Rs.getdouble ("Price");


Getresultset (); ResultSet
Returns the result set, or null if NULL
Getupdatecount (); int returns the number of rows affected, if not updated, returns-1
Close (); Void closes the statement object and its corresponding result set
Isclose () Boolean If the statement is closed; true

3.java.sql.resultset (Result set action method)

Next () Boolean moves forward one line to the last line and returns false
getxxx (int columnnum); Xxx
GetXXX (String columnnum); XXXXXX refers to int double String Date
Findcolumn (String columnName); int to column name, return column number
Close (); void closes the current result set
Isclose () Boolean If the statement is closed true

4. Principles:

1> each Connection object can create one or more statement objects
The same statement object can be used with multiple unrelated commands and queries, but only one result set is opened

The Close method should be called immediately after the 2> is exhausted

3> If the statement object has an open result set, the result set is automatically closed after the call Close ().
If the connection class closes all statements on the connection

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.