* 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