当前位置: 代码迷 >> Java Web开发 >> jdbc连接数据库问题
  详细解决方案

jdbc连接数据库问题

热度:463   发布时间:2010-04-06 15:50:18.0
jdbc连接数据库问题
我的代码:
<%@ 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);    //连接数据库
        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>
可是老提示

   An error occurred at line: 11 in the jsp file: /Ex5_1.jsp
conn cannot be resolved
8: <title>创建数据库</title>
9: </head>
10: <body ><font size=2>
11:   <%   
12:     try{
13:         
14:         Class.forName("oracle.jdbc.driver.OracleDriver");                                //载入JDBC驱动程序


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.

老大们帮帮我,小弟谢了!
搜索更多相关主题的帖子: jdbc  数据库  

----------------解决方案--------------------------------------------------------
貌似缺少数据库的登录名和密码!
----------------解决方案--------------------------------------------------------
回复 2楼 Bosen
登录名和密码是用sql*plus里的吗?还是自己随便设置
----------------解决方案--------------------------------------------------------
登录名和密码是你sql里面的,你有没有放jar包啊
----------------解决方案--------------------------------------------------------
jdbc的jar包啊
----------------解决方案--------------------------------------------------------
你放入JAR了吗?
----------------解决方案--------------------------------------------------------
jar是不是驱动程序?classes12。jar,我放到tomcat的路径里了
----------------解决方案--------------------------------------------------------
出现什么问题?没有说明我们无法更号的帮你哦
----------------解决方案--------------------------------------------------------
确定有jar包?
----------------解决方案--------------------------------------------------------
  An error occurred at line: 11 in the jsp file: /Ex5_1.jsp
conn cannot be resolved
8: <title>创建数据库</title>
9: </head>
10: <body ><font size=2>
11:   <%   
12:     try{
13:         
14:         Class.forName("oracle.jdbc.driver.OracleDriver");                                //载入JDBC驱动程序


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.
这是错误提示!

----------------解决方案--------------------------------------------------------
  相关解决方案