代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class LoadingDriverDemo {
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:mldn";
public static final String DBUSER = "scott";
public static final String PASSWORD = "tiger";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//第一步:加载数据库驱动程序
Class.forName(DBDRIVER);//向容器中加载驱动中的连接类
//第二步:取得数据库连接对象
Connection conn = DriverManager.getConnection(DBURL,DBUSER,PASSWORD);
System.out.println("数据库已连接");
//第三步:取得数据库连接对象
Statement stmt = conn.createStatement();//创建数据库
String sql = "INSERT INTO member(id,name,age,birthday,note)"
+"VALUES(myseq.nextval,'张三',"
+"10,TO_DATE('1990-10-01','yyyy-MM-dd'),暂无";
int len = stmt.executeUpdate(sql); //执行更新,返回更新行数
System.out.println("影响的数据行数"+len);
//第四步:关闭数据库连接
conn.close();
}
}
错误提示:
Exception in thread "main" java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:412)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:531)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:221)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.yige.JDBC.LoadingDriverDemo.main(LoadingDriverDemo.java:17)
Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:385)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1042)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:301)
... 7 more