/*odbc Use steps: (ODBC data source provided by Microsoft platform)
* 1. Configure an ODBC data source (Control Panel, Administrative tools->ODBC data source)
* 2, load and register the driver, import the java.sql.* package
* 3. Create a Connection object
* 4. Create execution Command object
* 5. Execute SQL statement
* 6, get the result after executing SQL statement, do related operation
* 7. Close Resources
*/
public class Odbc_demo {
ODBC Connect SQL Server
public static void Main (string[] args) {
try {
Loading and registering drivers
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
// }
String sql = "SELECT * from Stuinfo";
Connection conn = null;
Statement stm = null;
try {
Create a Connection object
conn = Drivermanager.getconnection ("Jdbc:odbc:mysqlserver", "sa", "Sasa");
To create an execution command object
STM = Conn.createstatement ();
Execute SQL statement
ResultSet rs = stm.executequery (SQL);
Gets the result after executing the SQL statement, doing the related operation
Traverse ResultSet
while (Rs.next ()) {
System.out.println (Rs.getint ("Stunum"));//Use column names to get the information in a cell
System.out.println (rs.getstring (2));//Use the column ordinal to get the information in the cell, note: The column ordinal starts at 1
System.out.println (Rs.getint ("Stuage"));
// }
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
Close Resource
if (stm!=null) {
Stm.close ();
// }
if (conn!=null) {
Conn.close ();
// }
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
// }
// }
// }
}