我想知道为什么Eclipse与’oracle数据库’之间的连接无法正常工作。

huangapple 未分类评论42阅读模式
英文:

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 &lt;Resource&gt; tag in Servers/Tomcat v9.0 Server at localhost-config/context.xml

This is the context I added

&lt;Resource
 name = &quot;jdbc/oracle&quot;
 auth = &quot;Container&quot;
 type = &quot;javax.sql.DataSource&quot;
 driverClassName = &quot;oracle.jdbc.OracleDriver&quot;
 url = &quot;jdbc:oracle:thin:@localhost:1521:XE&quot;
 username = &quot;scott&quot;
 password = &quot;tiger&quot;
 maxActive = &quot;50&quot;
 maxWait = &quot;-1&quot; /&gt;

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(&quot;java:/comp/env&quot;);
            dataFactory = (DataSource) envContext.lookup(&quot;jdbc/oracle&quot;);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List&lt;MemberVO&gt; listMembers() {
        List&lt;MemberVO&gt; list = new ArrayList&lt;MemberVO&gt;();
        try {
            
            con = dataFactory.getConnection();
            String query = &quot;select * from t_member &quot;;
            System.out.println(&quot;prepareStatememt: &quot; + query);
            pstmt = con.prepareStatement(query);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                String id = rs.getString(&quot;id&quot;);
                String pwd = rs.getString(&quot;pwd&quot;);
                String name = rs.getString(&quot;name&quot;);
                String email = rs.getString(&quot;email&quot;);
                Date joinDate = rs.getDate(&quot;joinDate&quot;);
                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 = &quot;insert into t_member&quot;;
            query += &quot; (id,pwd,name,email)&quot;;
            query += &quot; values(?,?,?,?)&quot;;
            System.out.println(&quot;prepareStatememt: &quot; + 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 = &quot;delete from t_member&quot; + &quot; where id=?&quot;;
            System.out.println(&quot;prepareStatememt:&quot; + 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(&quot;jdbc.url&quot;);
			String tns_location = info.getProperty(&quot;tns_location&quot;);
			//System.out.println(tns_location);
			String wallet_location = info.getProperty(&quot;wallet_location&quot;);
			
		    
		    //System.setProperty(&quot;oracle.net.tns_admin&quot;, &quot;C:\\app\\aatif-pc\\product\\18.0.0\\dbhomeXE\\network\\admin&quot;);
		    System.setProperty(&quot;oracle.net.tns_admin&quot;, tns_location);
		    //System.setProperty(&quot;oracle.net.wallet_location&quot;, &quot;(SOURCE=(METHOD=FILE)(METHOD_DATA=(DIRECTORY=C:\\app\\aatif-pc\\product\\18.0.0\\dbhomeXE\\owm\\wallets\\aatif-pc)))&quot;);
		    System.setProperty(&quot;oracle.net.wallet_location&quot;, wallet_location);		    
		    //String DB_URL=&quot;jdbc:oracle:thin:/@test&quot;;
		    
			      
			    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(&quot;Driver name: &quot; + dm.getDriverName());
				System.out.println(&quot;Driver version: &quot; + dm.getDriverVersion());
				System.out.println(&quot;Product name: &quot; + dm.getDatabaseProductName());
				System.out.println(&quot;Product version: &quot; + dm.getDatabaseProductVersion());
			}

huangapple
  • 本文由 发表于 2020年7月24日 15:12:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/63068602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定