不知道为什么,运行时提示这里有错误,搞了半天也搞不好,
//得到连接
Connection ct=DriverManager.getConnection("jdbc:odbc:testsp","scott","123");
运行时提示:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getConnection(String, String, String) is undefined for the type DriverManager
package com.sp;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
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
}
}
}
------解决方案--------------------
import java.sql.DriverManager ;
没有把类引用进来, 难道你能编译过去不报错 ?
------解决方案--------------------
- Java code
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 stubtry {//加载驱动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}}}
------解决方案--------------------
我连接成功的例子:
- Java code
<%@ 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>
------解决方案--------------------
- Java code
<%@ 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>