ODBC Operational database

Source: Internet
Author: User

/*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 ();
//   }
//  }
// }

}

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.