dbName指数据库名,user password是指你登录dbName的用户名和密码.

解决方案 »

  1.   

    String url="jdbc:mysql://localhost/"+dbName+"?user=root&password="";
      

  2.   

    如果数据库没有设置密码就用你登录mysql的用户名,一般为root.与jsp连接可以这样写
    try{
       Connection con;
       Statement stmt;(或PreparedStatement)
       ResultSet rs;
      //加载驱动程序
       Class.forName("com.mysql.jdbc.Driver");//这个是新的
       //Class.forName("org.gjt.mm.mysql.Driver");这个是旧的
      //注册mysql驱动程序
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      String url="jdbc:localhost:3306(端口)/dbName";
      String user="root";(username)
      String password=userpassword;
      con=DriverManager.getConnection(url,user,password);
      stmt=con.createStatement();
      rs=stmt.executeQuery(select * from table);
    .................
    用google搜一下,网上有很多这方面的资料.
      

  3.   

    <%@ page contentType="text/html;charset=gb2312"%> 
    <%@ page import="java.sql.*"%> 
    <html> 
    <body> 
    <%Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
    Connection con;
       Statement stmt;//(或PreparedStatement)
       ResultSet rs;
      //加载驱动程序
       Class.forName("com.mysql.jdbc.Driver");//这个是新的
       //Class.forName("org.gjt.mm.mysql.Driver");这个是旧的
      //注册mysql驱动程序
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      String url="jdbc:localhost:3306(端口)/first";
      String user="root";//(username)
      String password="00";
      con=DriverManager.getConnection(url,user,password);
      stmt=con.createStatement();
      rs=stmt.executeQuery(select * from about);
    while(rs.next()) {%> 
    您的第一个字段内容为:<%=rs.getString(1)%> 
    您的第二个字段内容为:<%=rs.getString(2)%> 
    <%}%> 
    <%out.print("数据库操作成功,恭喜你");%> 
    <%rs.close(); 
    stmt.close(); 
    con.close(); 
    %> 
    </body> 
    </html>这样的页面不能连接数据库
    不知为何
    错误如下:
    message description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: 5 in the jsp file: /first.jspGenerated servlet error:
        [javac] Compiling 1 source fileC:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\first_jsp.java:56: ')' expected
      rs=stmt.executeQuery(select * from about);
                                         ^
    1 error
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
    org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    note The full stack trace of the root cause is available in the Tomcat logs.
      

  4.   

    难道是(select * from about)改为(("select * from about"))?
    又出现错误:
    org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: 5 in the jsp file: /first.jspGenerated servlet error:
        [javac] Compiling 1 source fileC:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\first_jsp.java:50: package com.mysql.jdbc does not exist
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                                                     ^
    1 error
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
    org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    note The full stack trace of the root cause is available in the Tomcat logs.
      

  5.   

    是啊,rs=stmt.executeQuery(select * from about);里面是String来的.你改成这样试试.
    <%@ page contentType="text/html;charset=gb2312"%> 
    <%@ page import="java.sql.*"%> 
    <html> 
    <body> 
    <%
    Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
    Connection con;
       Statement stmt;//(或PreparedStatement)
       ResultSet rs;
      //加载驱动程序
       //Class.forName("com.mysql.jdbc.Driver");//这个是新的
       //Class.forName("org.gjt.mm.mysql.Driver");这个是旧的
      //注册mysql驱动程序
      //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      String url="jdbc:localhost:3306/first";
      String user="root";//(username)
      String password="00";
      con=DriverManager.getConnection(url,user,password);
      stmt=con.createStatement();
      rs=stmt.executeQuery("select * from about");
    while(rs.next()) {%> 
    您的第一个字段内容为:<%=rs.getString(1)%> 
    您的第二个字段内容为:<%=rs.getString(2)%> 
    <%}%> 
    <%out.print("数据库操作成功,恭喜你");%> 
    <%rs.close(); 
    stmt.close(); 
    con.close(); 
    %> 
    </body> 
    </html>因为你一开始就用了旧的驱动,但是后面又用新的,这样造成了混淆.
    //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    这一句我一直都没有写的,好像是已经注册了的,所以可以省略.
    你写的时候最好用上try...catch语句,追踪一下可能出错的地方,比如说连不数据库又怎样处理.