日期:2014-05-20 浏览次数:20935 次
package com.sp; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager ; public class TestOra { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { //加载驱动 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //得到连接 Connection ct=DriverManager.getConnection("jdbc:odbc:testsp","scott","123"); //从下面开始和sql server 一模一样 Statement sm=ct.createStatement(); ResultSet rs=sm.executeQuery("select * from emp"); while(rs.next()){ //用户名 System.out.println("用户名 : "+rs.getString(1)); } }catch (Exception e){ e.printStackTrace(); //TODO: handle exception } } }
------解决方案--------------------
我连接成功的例子:
<%@ page contentType="text/html;charset=GB2312"%> <%@ import="java.sql.*"%> <html><body> <% Connection con =null; Statement sql = null; ResultSet rs = null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); }catch(ClassNotFoundException e){ } try{ con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:","scott","tiger"); sql = con.createStatement(); rs = sql.executeQuery("select * from emp"); out.print("<table border>"); out.print("<tr>"); out.print("<th width=100>"+"empno"); out.print("<th width=100>"+"ename"); out.print("</tr>"); while(rs.next()){ out.print("<tr>"); int n = rs.getInt(1); out.print("<td>" + n + "</td>"); int e = rs.getInt(2); out.print("<td>" + e + "</td>"); out.print("<tr>"); } out.print("</table>"); con.close(); }catch(SQLException e){ out.print(" "+e); } %> </body></html>
------解决方案--------------------
<%@ page contentType="text/html;charset=GB2312"%> <%@ import="java.sql.*"%> <html><body> <% Connection con =null; Statement sql = null; ResultSet rs = null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); }catch(ClassNotFoundException e){ } try{ con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:这里是数据库名字","scott","tiger"); sql = con.createStatement(); rs = sql.executeQuery("select * from emp"); out.print("<table border>"); out.print("<tr>"); out.print("<th width=100>"+"empno"); out.print("<th width=100>"+"ename"); out.print("</tr>"); while(rs.next()){ out.print("<tr>"); int n = rs.getInt(1); out.print("<td>" + n + "</td>"); int e = rs.getInt(2); out.print("<td>" + e + "</td>"); out.print("<tr>"); } out.print("</table>"); con.close(); }catch(SQLException e){ out.print(" "+e); } %> </body></html>