如何使用 JSP 连接 Oracle 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23976779/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 05:39:31  来源:igfitidea点击:

How to connect Oracle Database Using JSP

oraclejspconnectivity

提问by Shakir Ali

I want to connect my Oracle database with web page using JSP. But data is not saving into database. if you have any idea or the code have any error please comment and show me the way to correct the the following code. Please this is the project of my final semester. The code is as follows. Thanks in advance.

我想使用 JSP 将我的 Oracle 数据库与网页连接。但是数据没有保存到数据库中。如果您有任何想法或代码有任何错误,请发表评论并向我展示更正以下代码的方法。请这是我最后一个学期的项目。代码如下。提前致谢。

<%@page import="java.sql.*"%>
<%@page import="oracle.jdbc.driver.*;" %>
<%@page import="oracle.sql.*;" %>
<%@page import="oracle.jdbc.driver.OracleDriver;"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<title>Auto Refresh Header Example</title>
</head>
<body>
<form>
<center>
<h1>Program Start</h1>
FirstName:<input type ="text" name ="firstName" id="firstName"> <br>

LastName:<input type="text" name ="lastname" id="lastname"> <br>

<input type="submit" value="Submit">
</form>
<%  
String username=request.getParameter("username");
String password =request.getParameter("username");

try {


Connection con = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "hr", "hr");
Statement st = null;
PreparedStatement ps=null;
ResultSet rs =null;
String query = "insert into Test (username, username)values           ('"+username+"','"+password+"')";

ps.executeUpdate(query);

} catch(Exception ex){

} 

%>
</center>
</body>

采纳答案by sai

try to open your server in admin mode,so that it can connect to database,hope it helps you

尝试以管理员模式打开您的服务器,以便它可以连接到数据库,希望对您有所帮助

回答by Carl Oliver

Within the try block I had to create statement for the connection and it appeared PreparedStatement wasn't necessary:

在 try 块中,我必须为连接创建语句,看起来 PreparedStatement 不是必需的:

Connection con = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "hr", "hr");
Statement st = con.createStatement();
String query = "insert into Test (username, username) values ('" + username + "','" + password + "')";

ps.executeUpdate(query);

回答by Ingamutuhanga Hekandjo

quickly get a close look at you values you have assigned two wrong parameters

快速仔细查看您分配了两个错误参数的值

first is "String password =request.getParameter("username");" which won't give you an error but data posted will be not that genuine, please change "username to "password"

首先是“String password =request.getParameter("username");” 这不会给你一个错误,但发布的数据不会那么真实,请将“用户名”更改为“密码”

Second is

其次是

String query = "insert into Test (username, username)values('"+username+"','"+password+"')";

which will be a runtime error hence the database will be waiting for a username and a password but you are supplying it with a username and a username. last try to us this connection string

这将是一个运行时错误,因此数据库将等待用户名和密码,但您正在为其提供用户名和用户名。最后尝试给我们这个连接字符串

Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr"); with the Class.forName being Class.forName("oracle.jdbc.OracleDriver");

回答by srujan

import java.sql.*;  
class InsertPrepared{  
public static void main(String args[]){  
try{  
Class.forName("oracle.jdbc.driver.OracleDriver");  

Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  

PreparedStatement stmt=con.prepareStatement("insert into Emp values(?,?)");  
stmt.setInt(1,101);//1 specifies the first parameter in the query  
stmt.setString(2,"Ratan");  

int i=stmt.executeUpdate();  
System.out.println(i+" records inserted");  

con.close();  

}catch(Exception e){ System.out.println(e);}  

}  
}