jdbc连接oracle数据库问题
<%@pagelanguage="java"contentType="text/html;charset=GB2312"pageEncoding="GB2312"%><%...
<%@ page language="java" contentType="text/html; charset=GB2312" pageEncoding="GB2312"%>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>创建数据库</title>
</head>
<body ><font size=2>
<%
try{
Class.forName("oracle.jdbc.driver.OracleDriver"); //载入JDBC驱动程序
String strCon = "jdbc:oracle:this:@localhost:1521: JspSamples"; //连接字
Connection conn = DriverManager.getConnection(strCon,"system","system"); //连接数据库
Statement stmt = conn.createStatement(); //初始化查询
String sql = "create database JspSamples"; //创建数据库
int ret = stmt.executeUpdate(sql); //执行创建数据库
String sql = "use JspSamples"; //选择数据库
int ret = stmt.executeUpdate(sql); //执行选择数据库
String sql = " CREATE TABLE customers(" + //创建表
" Id int(6) unsigned NOT NULL auto_increment," +
" Name varchar(20) default NULL," +
" addTime timestamp NULL default NULL," +
" Tel varchar(15) default NULL," +
" Email varchar(20) default NULL," +
" PRIMARY KEY (Id)" +
" )";
int ret = stmt.executeUpdate(sql); //执行创建表
}
catch(ClassNotFoundException e){ //意外处理,驱动程序无法找到
e.printStackTrace();
out.println("<h1>无法找到数据库驱动</h1>");
}
catch(SQLException e1){ //意外处理,数据库操作失败
e1.printStackTrace();
out.println("<h1>数据库操作失败</h1>");
}
out.println("<h1>新建数据库JspSamples成功</h1>");
DatabaseMetaData meta = conn.getMetaData(); //获取数据库信息
out.println("<br>");
out.println("连接字:" + meta.getURL()); //获取连接字
out.println("<br>");
out.println("数据库产品:" + meta.getDatabaseProductName()); //获取数据库产品名
out.println("<br>");
out.println("数据库版本:" + meta.getDatabaseProductVersion() ); //获取数据库产品版本
out.println("<br>");
out.println("驱动程序:" + meta.getDriverName() ); //获取数据库驱动名
out.println("<br>");
out.println("驱动程序版本:" + meta.getDriverVersion() ); //获取数据库驱动版本
stmt.close(); //关闭查询
conn.close(); //关闭连接
%>
</font>
</body>
</html>
提示错误:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 11 in the jsp file: /Ex5_1.jsp
Duplicate local variable sql
8: <title>创建数据库</title>
9: </head>
10: <body ><font size=2>
11: <%
12: try{
13:
14: Class.forName("oracle.jdbc.driver.OracleDriver");
希望老大们帮帮我 展开
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>创建数据库</title>
</head>
<body ><font size=2>
<%
try{
Class.forName("oracle.jdbc.driver.OracleDriver"); //载入JDBC驱动程序
String strCon = "jdbc:oracle:this:@localhost:1521: JspSamples"; //连接字
Connection conn = DriverManager.getConnection(strCon,"system","system"); //连接数据库
Statement stmt = conn.createStatement(); //初始化查询
String sql = "create database JspSamples"; //创建数据库
int ret = stmt.executeUpdate(sql); //执行创建数据库
String sql = "use JspSamples"; //选择数据库
int ret = stmt.executeUpdate(sql); //执行选择数据库
String sql = " CREATE TABLE customers(" + //创建表
" Id int(6) unsigned NOT NULL auto_increment," +
" Name varchar(20) default NULL," +
" addTime timestamp NULL default NULL," +
" Tel varchar(15) default NULL," +
" Email varchar(20) default NULL," +
" PRIMARY KEY (Id)" +
" )";
int ret = stmt.executeUpdate(sql); //执行创建表
}
catch(ClassNotFoundException e){ //意外处理,驱动程序无法找到
e.printStackTrace();
out.println("<h1>无法找到数据库驱动</h1>");
}
catch(SQLException e1){ //意外处理,数据库操作失败
e1.printStackTrace();
out.println("<h1>数据库操作失败</h1>");
}
out.println("<h1>新建数据库JspSamples成功</h1>");
DatabaseMetaData meta = conn.getMetaData(); //获取数据库信息
out.println("<br>");
out.println("连接字:" + meta.getURL()); //获取连接字
out.println("<br>");
out.println("数据库产品:" + meta.getDatabaseProductName()); //获取数据库产品名
out.println("<br>");
out.println("数据库版本:" + meta.getDatabaseProductVersion() ); //获取数据库产品版本
out.println("<br>");
out.println("驱动程序:" + meta.getDriverName() ); //获取数据库驱动名
out.println("<br>");
out.println("驱动程序版本:" + meta.getDriverVersion() ); //获取数据库驱动版本
stmt.close(); //关闭查询
conn.close(); //关闭连接
%>
</font>
</body>
</html>
提示错误:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 11 in the jsp file: /Ex5_1.jsp
Duplicate local variable sql
8: <title>创建数据库</title>
9: </head>
10: <body ><font size=2>
11: <%
12: try{
13:
14: Class.forName("oracle.jdbc.driver.OracleDriver");
希望老大们帮帮我 展开
2个回答
展开全部
存在不少问题..
String strCon = "jdbc:oracle:this:@localhost:1521: JspSamples"; //连接字
/*url写错了,这样:jdbc:oracle:thin:@localhost:1521: JspSamples(如1楼所说是thin不是this)*/
Connection conn = DriverManager.getConnection(strCon,"system","system"); //连接数据库
Statement stmt = conn.createStatement(); //初始化查询
String sql = "create database JspSamples"; //创建数据库
/*---> 上面这句不存在java语法问题,但是该sql对Oracle来说是执行不了的,自己再去了解一下Oracle吧*/
int ret = stmt.executeUpdate(sql); //执行创建数据库
String sql = "use JspSamples"; //选择数据库
/*---> 重复定义变量sql,把String去掉,且与上面那句存在相同问题(oracle不认识 use..,这不是oracle的语法)*/
int ret = stmt.executeUpdate(sql); //执行选择数据库
String sql = " CREATE TABLE customers(" + //创建表
/*---> 同上,把String去掉*/
" Id int(6) unsigned NOT NULL auto_increment," +
" Name varchar(20) default NULL," +
" addTime timestamp NULL default NULL," +
" Tel varchar(15) default NULL," +
" Email varchar(20) default NULL," +
" PRIMARY KEY (Id)" +
" )";
int ret = stmt.executeUpdate(sql); //执行创建表
String strCon = "jdbc:oracle:this:@localhost:1521: JspSamples"; //连接字
/*url写错了,这样:jdbc:oracle:thin:@localhost:1521: JspSamples(如1楼所说是thin不是this)*/
Connection conn = DriverManager.getConnection(strCon,"system","system"); //连接数据库
Statement stmt = conn.createStatement(); //初始化查询
String sql = "create database JspSamples"; //创建数据库
/*---> 上面这句不存在java语法问题,但是该sql对Oracle来说是执行不了的,自己再去了解一下Oracle吧*/
int ret = stmt.executeUpdate(sql); //执行创建数据库
String sql = "use JspSamples"; //选择数据库
/*---> 重复定义变量sql,把String去掉,且与上面那句存在相同问题(oracle不认识 use..,这不是oracle的语法)*/
int ret = stmt.executeUpdate(sql); //执行选择数据库
String sql = " CREATE TABLE customers(" + //创建表
/*---> 同上,把String去掉*/
" Id int(6) unsigned NOT NULL auto_increment," +
" Name varchar(20) default NULL," +
" addTime timestamp NULL default NULL," +
" Tel varchar(15) default NULL," +
" Email varchar(20) default NULL," +
" PRIMARY KEY (Id)" +
" )";
int ret = stmt.executeUpdate(sql); //执行创建表
网易云信
2023-12-06 广告
2023-12-06 广告
网易云信提供一站式的 1 对 1 UIKit 组件库,可以更快地搭建 1 对 1 社交平台,能够快速实现音视频呼叫、音视频通话、1对1消息发送、美颜和礼物功能,直接可以复用我们的组件源码就可以了。优势:1、全套1对1 UI组件,接入更快;2...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询