英文:
I wonder why connection between eclipse and 'oracle database' doesn't work
问题
以下是翻译好的部分:
我试图在 Eclipse 中连接 Oracle 的 sqlplus。
我已经检查过我在 'WEB-INF/lib' 文件夹中添加了文件 'ojdbc6.jar' 和 'tomcat-dbcp-7.0.30.jar'。
我还在 Servers/Tomcat v9.0 Server at localhost-config/context.xml 中添加了 <Resource>
标签。
这是我添加的上下文:
<Resource
name="jdbc/oracle"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:XE"
username="scott"
password="tiger"
maxActive="50"
maxWait="-1" />
这是与 SQL 连接相关的代码部分:
package sec02.ex02;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class MemberDAO {
private Connection con;
private PreparedStatement pstmt;
private DataSource dataFactory;
public MemberDAO() {
try {
Context ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:/comp/env");
dataFactory = (DataSource) envContext.lookup("jdbc/oracle");
} catch (Exception e) {
e.printStackTrace();
}
}
public List<MemberVO> listMembers() {
// ... (代码在这里被省略)
return list;
}
public void addMember(MemberVO memberVO) {
// ... (代码在这里被省略)
}
public void delMember(String id) {
// ... (代码在这里被省略)
}
}
当然,我是按照书上的写法来编写的。
首先,我期望的输出是这样的:
id password name email joindate
aaa 1234 tom aaa@google.com 3/3
但是我实际看到的输出却是这样的:
id password name email joindate
(nothing appears)
我在 sqlplus 中添加了正确的表,保存了它,并且在 sqlplus 运行和未运行的情况下都尝试过。
我不知道哪里出了问题。
英文:
I tried to connect oracle sqlplus with eclipse
I already checked that I added the files 'ojdbc6.jar' and 'tomcat-dbcp-7.0.30.jar' in 'WEB-INF/lib' folder
I also added <Resource>
tag in Servers/Tomcat v9.0 Server at localhost-config/context.xml
This is the context I added
<Resource
name = "jdbc/oracle"
auth = "Container"
type = "javax.sql.DataSource"
driverClassName = "oracle.jdbc.OracleDriver"
url = "jdbc:oracle:thin:@localhost:1521:XE"
username = "scott"
password = "tiger"
maxActive = "50"
maxWait = "-1" />
and this is the part of code related to connection with sql
package sec02.ex02;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class MemberDAO {
private Connection con;
private PreparedStatement pstmt;
private DataSource dataFactory;
public MemberDAO() {
try {
Context ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:/comp/env");
dataFactory = (DataSource) envContext.lookup("jdbc/oracle");
} catch (Exception e) {
e.printStackTrace();
}
}
public List<MemberVO> listMembers() {
List<MemberVO> list = new ArrayList<MemberVO>();
try {
con = dataFactory.getConnection();
String query = "select * from t_member ";
System.out.println("prepareStatememt: " + query);
pstmt = con.prepareStatement(query);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String id = rs.getString("id");
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String email = rs.getString("email");
Date joinDate = rs.getDate("joinDate");
MemberVO vo = new MemberVO();
vo.setId(id);
vo.setPwd(pwd);
vo.setName(name);
vo.setEmail(email);
vo.setJoinDate(joinDate);
list.add(vo);
}
rs.close();
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public void addMember(MemberVO memberVO) {
try {
con = dataFactory.getConnection();
String id = memberVO.getId();
String pwd = memberVO.getPwd();
String name = memberVO.getName();
String email = memberVO.getEmail();
String query = "insert into t_member";
query += " (id,pwd,name,email)";
query += " values(?,?,?,?)";
System.out.println("prepareStatememt: " + query);
pstmt = con.prepareStatement(query);
pstmt.setString(1, id);
pstmt.setString(2, pwd);
pstmt.setString(3, name);
pstmt.setString(4, email);
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void delMember(String id) {
try {
con = dataFactory.getConnection();
String query = "delete from t_member" + " where id=?";
System.out.println("prepareStatememt:" + query);
pstmt = con.prepareStatement(query);
pstmt.setString(1, id);
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Certainly I wrote it following what is written in book
First, what I expected is this
id password name email joindate
aaa 1234 tom aaa@google.com 3/3
but all I can see is this
id password name email joindate
(nothing appears)
I added correct table in sqlplus, and save it, and I tried it while sqlplus is running and not running both.
I don't know what's wrong.
答案1
得分: -2
请确保您能够从Eclipse连接到Oracle数据库,尝试简单的连接。以下是示例代码,我已经使用了属性文件,但您也可以使用直接连接。
Properties info = new Properties();
info.load(input);
String DB_URL = info.getProperty("jdbc.url");
String tns_location = info.getProperty("tns_location");
String wallet_location = info.getProperty("wallet_location");
System.setProperty("oracle.net.tns_admin", tns_location);
System.setProperty("oracle.net.wallet_location", wallet_location);
OracleDataSource ods = new OracleDataSource();
ods.setURL(DB_URL);
ods.setConnectionProperties(info);
OracleConnection conn = (OracleConnection) ods.getConnection();
System.out.println(DB_URL);
if (conn != null) {
DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
System.out.println("Driver name: " + dm.getDriverName());
System.out.println("Driver version: " + dm.getDriverVersion());
System.out.println("Product name: " + dm.getDatabaseProductName());
System.out.println("Product version: " + dm.getDatabaseProductVersion());
}
英文:
First try to make sure you are able to connect to Oracle database from Eclipse, try simple connect.Sample code below, I have used the proporty file but you can use direct connect as well.
Properties info = new Properties();
info.load(input);
String DB_URL = info.getProperty("jdbc.url");
String tns_location = info.getProperty("tns_location");
//System.out.println(tns_location);
String wallet_location = info.getProperty("wallet_location");
//System.setProperty("oracle.net.tns_admin", "C:\\app\\aatif-pc\\product\\18.0.0\\dbhomeXE\\network\\admin");
System.setProperty("oracle.net.tns_admin", tns_location);
//System.setProperty("oracle.net.wallet_location", "(SOURCE=(METHOD=FILE)(METHOD_DATA=(DIRECTORY=C:\\app\\aatif-pc\\product\\18.0.0\\dbhomeXE\\owm\\wallets\\aatif-pc)))");
System.setProperty("oracle.net.wallet_location", wallet_location);
//String DB_URL="jdbc:oracle:thin:/@test";
OracleDataSource ods = new OracleDataSource();
ods.setURL(DB_URL);
ods.setConnectionProperties(info);
OracleConnection conn = (OracleConnection) ods.getConnection();
System.out.println(DB_URL);
if (conn != null) {
DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
System.out.println("Driver name: " + dm.getDriverName());
System.out.println("Driver version: " + dm.getDriverVersion());
System.out.println("Product name: " + dm.getDatabaseProductName());
System.out.println("Product version: " + dm.getDatabaseProductVersion());
}
专注分享java语言的经验与见解,让所有开发者获益!
评论