drivers=oracle.jdbc.driver.OracleDriver
jdbcurl=jdbc:oracle:thin:@192.168.9.22:1521:orcl
username=system
password=manager
jdbcurl=jdbc:oracle:thin:@192.168.9.22:1521:orcl
username=system
password=manager
解决方案 »
- 简单的集合问题
- 高手请进
- what 数据抽象 and 过程抽象
- 有谁能解释一下Vector的indexOf方法调用哪个equals方法?
- 请问这条语句创建了几个String Object
- jfreechart区域图怎么样支持鼠标在区域上任意点都可以有提示小窗口
- 困惑了我好久的问题
- Java StringBuilder和String的内存分析的问题
- 请介绍几个好的JBuilder网站,国内的和国外的
- 鸟鸟问题:为什么我想打开数据库时,说找不到jdbc driver??
- String x="abc";if(x.equals("abc")) x="uvw";那么x就变成"uvw",String对象不是不能修改吗?为什么会这样?
- 客户端连接oracle服务器!!
DriverManager.getConnection(url, username, pwd);
我在使用
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
时说找不到oracledriver().
我把下载的oracle 9i的jdbc 驱动classes12.zip 压成classes12.jar
并放在d:\jdk\lib目录下,然后把类路径
set classpath=%classpath%;d:\jdk\lib\classes12.jar
不过,编译没问题,运行时还是报错import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.driver.*;
class Employee
{
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new OracleDriver()); // Connect to the database
// You must put a database name after the @ sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as <host>:<port>:<sid>. The example uses the short cut syntax.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:thin:@foxconncsu:1521:hotmail",
"scott", "tiger"); // Create a Statement
Statement stmt = conn.createStatement (); // Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select ENAME from EMP"); // Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1));
}
}
stmt.close();
conn.close();关闭资源是个好习惯,很重要
首先你不该把下载的CLASSES12.zip压缩成JAR,然后在CLASSPTH中直接加入:set classpath=%classpath%;d:\jdk\lib\classes12.zip
这下就应该可以了!
运行后
Io 异常: The Network Adapter could not establish the connectionException
n thread "main" java.lang.NullPointerException
at conndb.main(conndb.java:36)能解释下怎么回事吗???
stmt.close();
conn.close();
String strDriver = "oracle.jdbc.driver.OracleDriver";
String strService = "jdbc:oracle:thin:@服务器IP:端口:服务名";
String strUserID = "用户名";
String strPassword = "密码";
Class.forName(strDriver).newInstance();
m_connection = (Connection)DriverManager.getConnection(strService, strUserID, strPassword);
把代码拿上来看看!
{
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
}catch(SQLException ex){
System.err.print("aaaaaaaaaaa" + ex.getMessage());
}
// Connect to the database
Connection conn=null;
Statement stmt=null;
try{
conn =DriverManager.getConnection ("jdbc:oracle:thin:@10.134.11.66:1521:hotmail",
"pdm", "tartan");}
catch(SQLException ex){
System.err.print("dddddddd" + ex.getMessage());
} // Create a Statement
stmt = conn.createStatement (); // Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select ENAME from EMP"); // Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1));
}
}
运行后:
Io 异常: The Network Adapter could not establish the connectionException
n thread "main" java.lang.NullPointerException
at conndb.main(conndb.java:36)
stmt = conn.createStatement ();这句前面你最好判断一下
conn是否为null;
不为null的话再执行这一句。
import java.util.*;
import java.sql.*;
import javax.sql.*;
import oracle.jdbc.pool.*;
import javax.naming.*;
import oracle.jdbc.driver.*;public class DBConnectionPool
{
private OracleConnectionCacheImpl m_DBConnectionPool;
public DBConnectionPool()
{
this.createConnectionPool(); } private void createConnectionPool()
{
BufferedReader in = null;
try
{
OracleConnectionPoolDataSource l_ocpds = new OracleConnectionPoolDataSource(); String path ="";
String connstr = "";
String user = "";
String password = "";
int idx = 0;
path = System.getProperty("user.dir");
in = new BufferedReader(new FileReader(path+"\\DBInfomation.ini"));
if((connstr = in.readLine ()) != null)
{
idx = connstr.indexOf ("=");
if(idx > 0)
connstr = connstr.substring (idx + 1).trim();
else
connstr = "";
}
else
connstr = "";
if((user = in.readLine ()) != null)
{
idx = user.indexOf ("=");
if(idx > 0)
user = user.substring (idx + 1).trim();
else
user = "";
}
else
user = "";
if((password = in.readLine ()) != null)
{
idx = password.indexOf ("=");
if(idx > 0)
password = password.substring (idx + 1).trim();
else
password = "";
}
else
password = ""; l_ocpds.setURL(connstr);
l_ocpds.setUser(user); l_ocpds.setPassword(password);
m_DBConnectionPool = new OracleConnectionCacheImpl(l_ocpds);
m_DBConnectionPool.setMaxLimit (Parameter.iConnectionMax);
(OracleConnectionCacheImpl.FIXED_RETURN_NULL_SCHEME);
(OracleConnectionCacheImpl.FIXED_WAIT_SCHEME );
m_DBConnectionPool.setCacheScheme (OracleConnectionCacheImpl.DYNAMIC_SCHEME ); if (m_DBConnectionPool != null )
System.out.println("Connection Pool Created Succeefully");
else
System.out.println("Error in Creating Connection Pool");
}
catch(SQLException ex)
{
System.out.println( "Error in Connecting to the Database "+'\n'+ex.toString());
}
catch(FileNotFoundException ex)
{
System.out.println("user.dir is " + System.getProperty("user.dir"));
System.out.println("java.home is " + System.getProperty("java.home"));
System.out.println( "Error in open DBInfo.ini "+'\n'+ex.toString());
}
catch(IOException ex)
{
System.out.println( "Error in read DBInfo.ini "+'\n'+ex.toString());
}
finally
{
try
{
if(in!=null)
in.close();
}
catch(Exception ex)
{;}
}
} public Connection getConnection()
{
Connection l_connection = null;
try
{
if( m_DBConnectionPool != null )
{
l_connection = m_DBConnectionPool.getConnection();
}
else
{
System.out.println("m_DBConnectionPool is Null");
l_connection = null;
}
}
catch(SQLException ex)
{
System.out.println("Error in getting the connection "+ex.toString());
l_connection = null;
}
catch(Exception ex)
{
System.out.println("Error in getting the connection "+ex.toString());
l_connection = null;
}
return l_connection;
} public void putConnection(Connection l_connection)
{
if (l_connection != null)
{
try
{
l_connection.close(); } catch (SQLException ex)
{
System.out.println("Error in closing the connection "+ex.toString());
}
}
}
protected void finalize() throws Throwable
{
if (m_DBConnectionPool != null)
{
try
{
m_DBConnectionPool.close(); }
catch (SQLException e)
{
System.out.println("Error in closing the pooled connection "+e.toString());
}
}
}
}
//DBInfomation.ini file DBConnStr=jdbc:oracle:thin:@host:number:orcl
DBUser=1111
DBPassword=1111
String strDriver = "oracle.jdbc.driver.OracleDriver";
String strService = "jdbc:oracle:thin:@服务器IP:端口:服务名";
String strUserID = "用户名";
String strPassword = "密码";
Class.forName(strDriver).newInstance();
m_connection = (Connection)DriverManager.getConnection(strService, strUserID, strPassword);