In the afternoon, I spent some time programming java & access. In fact, the company needs to integrate the data of two heterogeneous systems. One database uses ms SQL server and the other is access. In fact, operations on data tables are always an important step in the program. Previously, the company had been using SQL server, and database access operations were encapsulated with javabean, and most companies actually did the same.
For programming, for SQL server, Microsoft provides a dedicated jdbc driver, that is, the three jar files that are commonly used. However, access programming is not so luxurious. Currently, the available programming methods only provide a jdbc-odbc bridge for connection, so the efficiency is not required.
The process is also a bit rough:
1. Create a database URL: configure the access data source at the same time.
For example, String url = "jdbc: odbc: Oblog ";
2. Load the driver
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
3. Create a connection
Connection conn = DriverManager. getConnection (url, "user", "pwd ");
4. Create an SQL statement object
Statement stmt = conn. createStatement ();
5. Execute SQL statements
Stmt.exe cute (strSql. toString ());
Simply paste an example:
|
// Select. java
Import java. SQL .*;
Class Select { Public static void main (){ Try { String url = "jdbc: odbc: wombat "; Connection conn = DriverManager. getConnection (url, "user", "pwd "); Statement stmt = conn. createStatement (); ResultSet rs = stmt.exe cuteQuery ("SELECT * FROM Table1 "); System. out. println ("Got ResultSet Now "); Rs. beforeFirst (); While (rs. next ()){ System. out. println (rs. getString (1 )); } Stmt. close (); Conn. close (); } Catch (Exception ex ){ Ex. printStackTrace (); }
|