日期:2014-05-19 浏览次数:21037 次
编程实例: try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //(1)创建与数据库的连接 Connection con=DriverManager.getConnection("jdbc:odbc:DatabaseDSN","Login","Password"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from DBTableName");//(2)发送SQL语句到数据库中 while(rs.next()) { String name=rs.getString("Name") ; //(3)处理数据并查询结果。 int age=rs.getInt("age"); float wage=rs.getFloat("wage"); } rs.close(); //(4)关闭 stmt.close(); con.close(); } catch(SQLException e) { System.out.println("SQLState:"+ e.getSQLState()); System.out.println("Message:" + e.getMessage()); System.out.println("Vendor:" + e.getErrorCode()); }