如何使用JSP连接Oracle数据库

时间:2014-06-01 04:31:48

标签: oracle jsp connectivity

我想使用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>

4 个答案:

答案 0 :(得分:1)

在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);

答案 1 :(得分:1)

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

首先是“字符串密码= request.getParameter(“用户名”);“不会给您带来错误,但是发布的数据不是真实的,请将“用户名”更改为“密码”

第二个是

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

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

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

<?php
include_once "library/inc.sesadmin.php";
$row	= 25;
$hal 	= isset($_GET['hal']) ? $_GET['hal'] : 1;

if($usertype != 'Admission'||$usertype != 'Administrator'){
    
if($_GET) {
	$Kode	= $_GET['Kode'];
	$myQry=$db->prepare("SELECT * FROM student WHERE student_code='$Kode'");
	$myQry->execute();
    $myQryCount = $myQry->rowCount();

	while ($myData = $myQry->fetch()) {
	}
}

?>
<table >
        <tr>
      <td ><img src="foto/<?php echo $namaFoto; ?>"></td>
      <td ><b>Code</b></td>
      <td ><b>:</b></td>
      <td ><?php echo $myData['student_code']; ?></td>
    </tr>
    <tr>
      <td><b>Student ID </b></td>
      <td><b>:</b></td>
      <td> <?php echo $myData['student_id']; ?>  </td>
    </tr>
</table>
      <?php } else { ?>
      <br /> <br /> <br /> <br /> <br /> <br />
      <p align="center">You are not authorized to access this area</p>
      <?php } ?>