import java.io.*;
import java.sql.*;//NOTE:
//1. 不支持非标准SQL语句 "go"public class odbc{//SingleQuery ()
public static void SingleQuery (){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ce){
System.out.println("SQLException:" + ce.getMessage());
} try{
Connection con = DriverManager.getConnection("jdbc:odbc:MSSQL");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from Table_1"); while (rs.next()){
System.out.println(
rs.getString(1) + "\t" +
rs.getString(2) + "\t" +
rs.getString(3) + "\t" +
rs.getString(4)
);
} stmt.close();
con.close();
}
catch(SQLException sqle){
System.out.println("SQLException: " + sqle.getMessage());
}}//SingleUpdate()
public static void SingleUpdate(){}//CreateTab() 创建临时表
public static void CreateTab() throws Exception{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException ce){
System.out.println("SQLException:" + ce.getMessage());
} try{
Connection con = DriverManager.getConnection("jdbc:odbc:MSSQL");
Statement stmt = con.createStatement(); stmt.executeUpdate(
"if exists (select 1" +
" from sysobjects" +
" where id = object_id('tempTab')" +
" and type = 'U')" +
" drop table tempTab"
); stmt.executeUpdate(
"Create table tempTab(" +
"id varchar(10) not null," +
"c1 varchar(10) null," +
"c2 varchar(10) null," +
"c3 varchar(10) null," +
"c4 varchar(10) null," +
"c5 varchar(10) null," +
"constraint PK_tempTab primary key (id)" +
")"
); stmt.close();
con.close();
}
catch(SQLException sqle){
System.out.println("SQLException: " + sqle.getMessage());
}}
//main()
public static void main(String[] args) throws Exception{
BufferedReader br;
String s; br = new BufferedReader( new InputStreamReader(System.in) ); do{
System.out.print(">");
s = br.readLine(); //调用函数
if ( s.compareTo("query") == 0 ) SingleQuery();
else if ( s.compareTo("update") == 0 ) SingleUpdate();
else if ( s.compareTo("create") == 0 ) CreateTab(); //其它处理
else if ( s.compareTo("Exit") == 0 );
else System.out.println("命令错误!(请用小写)"); } while (s.compareTo("Exit") != 0); br.close();}}
Connection conn = null;
Statement stmt = null;
//记录集
ResultSet rset;
String query;
String update;try{
//加载JDBC驱动程序,采用JDBC+ODBC BRIDGE方式连接ACCESS数据库
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){}
}catch(Exception e){
//加载驱动程序出错的异常处理。
out.println("Class not found exception");
}try{
//创建一个数据库连接,jsp为数据源名(在ODBC里配置)
conn = DriverManager.getConnection("jdbc:odbc:aboutjsp");
//生成正文
stmt = conn.createStatement();
//执行SQL更新语句update
update = " INSERT INTO table (name,password) VALUES('001','Password') ";
stmt.executeUpdate(update);
//执行SQL查询语句,返回一个ResultSet
query = "SELECT * FROM table ";
rset = stmt.executeQuery(query);
//显示查询结果
while(rset.next()){
out.println(rset.getString(1));
}
}catch(SQLException e){
//SQL操作出错的异常处理
out.println("SQL exception!");}