JDBC-ODBC方式连接数据库

JDBC-ODBC方式连接数据库

package com.jdbc.odbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class jdbc_demo1 {

public static void main(String[] args) {
  
try {
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection ct = DriverManager.getConnection("jdbc:odbc:mytest","scott","tiger");   //mytest为数据源名称
   
       Statement st = ct.createStatement();
       ResultSet rs = st.executeQuery("select * from emp");
   
       while(rs.next()){
       
         System.out.println("用户名:"+rs.getString(2));
    
   }
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

你可能感兴趣的:(JDBC-ODBC方式连接数据库)