Comparison of JDBC connection to SQL server and ADO. NET connection to SQL Server
1. JDBC connection to SQL server
1) Currently, many java drivers can drive the connection to SQL servernet.
There are two mainstream sourceforge. jtds. jdbc. Driver and JDBC.
2) the code is as follows:
Package com. testSqlJDBC;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. Statement;
Public class testJDBC {
Public static void main (String [] srg ){
// Jtds connection
// Try {
//
// Class. forName ("net. sourceforge. jtds. jdbc. Driver"); // 1. Register and activate the Driver
// Connection conn = DriverManager. getConnection ("jdbc: jtds: sqlserver: // ZHAO-PC: 1433/Northwind", "sa", "1"); // 2. Connect to the database
// Statement stmt = conn. createStatement (); // 3. Open the database
// ResultSet result = stmt.exe cuteQuery ("select * from Orders"); // 4. Execute SQL
// While (result. next ()){
// System. out. println (result. getString ("ShipName "));
//}
// Stmt. close (); // 5. close the database connection
// Conn. close ();
//} Catch (Exception e ){
// E. printStackTrace ();
//}
Try {
// Jdbc connection
Class. forName ("com. microsoft. sqlserver. jdbc. SQLServerDriver ");
Connection conn = DriverManager. getConnection ("jdbc: sqlserver: // ZHAO-PC \ SQL2008R2S1: 12966; DatabaseName = CMSDB", "sa", "1 ");
Statement stmt = conn. createStatement ();
ResultSet result = stmt.exe cuteQuery ("select * from SYS_Code ");
While (result. next ()){
System. out. println (result. getString ("CodeName "));
}
Stmt. close ();
Conn. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
2. Comparison of connecting to SQL Server using ADO. NET
Public DataTable GetData ()
{
String connString = @ "Data Source = ZHAO-PC \ SQL2008R2S1; Initial Catalog = test; Persist Security Info = True; User ID = sa; Password = 1 ";
SqlConnection cn = new SqlConnection (connString );
Cn. Open (); // 1. Open the database connection
// 2. Execute SQL
SqlCommand cm = new SqlCommand ();
Cm. Connection = cn;
Cm. commandtype = commandtype. text;
Cm. commandtext = "select * From sys_code ";
Sqldataadapter da = new sqldataadapter ();
Da. selectcommand = cm;
Datatable dt = new datatable ();
Da. Fill (DT );
// 2. Close the database connection
CN. Close ();
Return DT;
}
3. Summary
1) both Java jdbc and. net ADO. NET implement database access interfaces.
2) the corresponding drivers must be used to access other databases. These drivers all implement database access interfaces defined in java or net.
3) by defining the interface specifications, different database vendors can provide their own database access interfaces (that is, drivers) in accordance with the specifications ). In this way, the database access code is consistent.
4) General steps for accessing the database: Open the database, execute SQL statements, and close the database.
5) both java and net can be used as web Services for android clients to call. In traditional android applications, database access is generally performed on the android front-end display + web service to access the database and provide data sources.