我是一个菜鸟,刚刚开始学习oracle,为什么在java中用jdbc写代码连接oracle没有任何反应,连异常都没有代码如下:
package com.zsy.test01;import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;public class JDBCTest { public Connection getConnection() throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
String driverClass =null;
String jdbcUrl=null;
String user =null;
String password=null;
InputStream in=
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);
driverClass=properties.getProperty("driver");
jdbcUrl=properties.getProperty("jdbcUrl");
user=properties.getProperty("user");
password=properties.getProperty("password");
Driver driver=
(Driver) Class.forName(driverClass).newInstance();
Properties info=new Properties();
info.put("user", user);
info.put("password", password);
Connection connection =driver.connect(jdbcUrl, info);
return connection;
}
public void testGetConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException, SQLException{
System.out.println(getConnection());
}
}
file文件中代码
package com.zsy.test01;import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;public class JDBCTest { public Connection getConnection() throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
String driverClass =null;
String jdbcUrl=null;
String user =null;
String password=null;
InputStream in=
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);
driverClass=properties.getProperty("driver");
jdbcUrl=properties.getProperty("jdbcUrl");
user=properties.getProperty("user");
password=properties.getProperty("password");
Driver driver=
(Driver) Class.forName(driverClass).newInstance();
Properties info=new Properties();
info.put("user", user);
info.put("password", password);
Connection connection =driver.connect(jdbcUrl, info);
return connection;
}
public void testGetConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException, SQLException{
System.out.println(getConnection());
}
}
file文件中代码
如果是ConnectionState ==ConnectionState.Open的话说明数据库连接成功并且是OPEN状态,可以做数据库操作了。
.....
private Connection conn = null;
public static void getConnect() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","sys as sysdba","sys ");
System.out.println(conn.toString());
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}