使用 jsp 创建与 Oracle 10g 的 JDBC 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3116761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating JDBC Connection with Oracle 10g using jsp
提问by Logan
I am able to connect to Oracle 10g (using ojdbc14.jar driver) with java. But when I use the same code in a servlet or file with .jsp extension, I am getting class not found exception. I am not able to understand why this is happening. Do we have different connection strings for JDBC in java and jsp? This is what I use to connect to oracle iwith both, java and jsp:
我能够使用 java 连接到 Oracle 10g(使用 ojdbc14.jar 驱动程序)。但是当我在扩展名为 .jsp 的 servlet 或文件中使用相同的代码时,我得到了 class not found 异常。我无法理解为什么会发生这种情况。java和jsp中的JDBC连接字符串是否不同?这是我使用 java 和 jsp 连接到 oracle i 的方法:
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:xe";
//Xe being the database name
String usr = "username";
String pwd = "pwd";
Works fine with java but gives error with jsp.
使用 java 工作正常,但使用 jsp 时出错。
回答by Pabitra Padhy
i found the solution to this...
我找到了解决方案...
it's very easy... i would be specific to oracle database and apache tomcat server.
这很容易...我将特定于 oracle 数据库和 apache tomcat 服务器。
download ojdbc6.jar from oracle or any source
从 oracle 或任何来源下载 ojdbc6.jar
place it in your tomcat/lib/ (no matter if you use xampp or any amp bundle, it is there) RESTART YOUR SERVER its a must, otherwise you would be wondering only lolz.
把它放在你的 tomcat/lib/(无论你是使用 xampp 还是任何 amp 包,它都在那里)重新启动你的服务器是必须的,否则你只会想知道 lolz。
try this code it works..!!
试试这个代码它的工作原理..!!
<%@ page import="java.sql.*" %>
<HTML>
<HEAD>
<TITLE>Simple JSP to Oracle connection Example</TITLE>
</HEAD>
<BODY>
<%
Connection conn = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
out.println("connected....!!");
}
catch(Exception e)
{
out.println("Exception : " + e.getMessage() + "");
}
%>
</BODY>
</HTML>
回答by duffymo
If you're using a CLASSPATH environment variable, your app server ignores it. That's probably why it "works" with Java but not with JSPs.
如果您使用的是 CLASSPATH 环境变量,您的应用服务器会忽略它。这可能就是为什么它“适用于”Java 而不适用于 JSP。
I'd advise you to not rely on CLASSPATH that way. Learn how to set it properly for every situation.
我建议您不要那样依赖 CLASSPATH。了解如何针对每种情况正确设置它。
I'd also advise against putting scriptlet code in JSPs. This will grow to be a maintenance nightmare in a short time.
我还建议不要将 scriptlet 代码放在 JSP 中。这将在短时间内发展成为维护噩梦。
If you must put database calls in a JSP, learn how to use JSTL and its <sql>
tags.
如果您必须将数据库调用放在 JSP 中,请了解如何使用 JSTL 及其<sql>
标记。
回答by wds
There should be no real difference between the two. Is the driver jar in your WEB-INF/lib/ subdirectory? A class not found exception typically means your jar wasn't found on the classpath.
两者之间应该没有真正的区别。驱动程序 jar 是否在您的 WEB-INF/lib/ 子目录中?未找到类异常通常意味着在类路径中找不到您的 jar。
回答by Guneet Kaur
<%@page import="java.sql.*"%>
<%@page import="oracle.jdbc.driver.*" %>
<%@page import="oracle.sql.*;" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="but1.jsp">
<label>
Sr No:<input type="text" name="txt" /></br>
Name: <input type="text" name="txt1" />
</label>
<p> </p>
<p>
<input type="submit" name="but1" id="but1" value="Submit">
</input>
<% String s=request.getParameter("txt");
System.out.print(s);
String s1=request.getParameter("txt1");
Connection con;
Statement st;
ResultSet rs;
try{
System.out.println("hi");
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("1");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","oracle-uname","oracle-password");
System.out.println("2");
st=con.createStatement();
System.out.println("3");
String query="insert into table1(srno, name) values ("+s+",'"+s1+"')";
System.out.println("4");
int rRs=st.executeUpdate(query);
System.out.println("5");
String q="Select * from table1 where srno="+s+"";
System.out.println("cats");
rs=st.executeQuery(q);
System.out.println("catty");
while(rs.next())
{
System.out.println("6");
out.println("number is: "+rs.getString(1));%>
</br>
<%out.print("name is: "+rs.getString(2));
}
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
%>
</body>
</html>
"jdbc:oracle:thin:@localhost:1521:XE"=path
"jdbc:oracle:thin:@localhost:1521:XE"=path
to set path.. services > databases > drivers > oracle thin > connect using > add jar ojdbc6.jar > next > jdbc url (at the bottom) > set uname to system, and password to oracle password > test connection
设置路径.. 服务> 数据库> 驱动程序> oracle 瘦> 连接使用> 添加jar ojdbc6.jar > 下一个> jdbc url(在底部)> 将uname 设置为系统,并将密码设置为oracle 密码> 测试连接
the jdbc url forms the path ie "jdbc:oracle:thin:@localhost:1521:XE"
jdbc url 形成路径,即“jdbc:oracle:thin:@localhost:1521:XE”
all the best
祝一切顺利