http://www.csdn.net/expert/topic/670/670716.xml?temp=.6757166

解决方案 »

  1.   

    谢谢Andrawu(Andrawu)和thebear810611(小雄)的肯定回答,弄了一晚上终于OK
    下面是我的调试方法,这是个书上的例子,比较容易找到错的原因。但是这一句,String url = "jdbc:mysql://localhost:3306/yundong";
    我当初填写的是机子的IP地址
    jdbc:mysql://192.168.0.173:3306/yundong
    没有成功,改成localhost才OK的,为什么呢?很想知道,谢谢!
    --------------------------------------------------------------------
    <%@page contentType="text/html" language="java" import="java.sql.*" errorPage="" %>
    <html>
    <head>
    <title>使用JDBC连接到数据库</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head><body>
    <%@ page import = "java.sql.*" %>
    <% 
       String driver= "org.gjt.mm.mysql.Driver";
       String url = "jdbc:mysql://localhost:3306/yundong";
       String userID="root";
       String passwd="xiaobao321";
       try
       {
       Class.forName(driver);
       }
       catch(Exception e)
       {
       out.println("无法栽入"+driver+"驱动程序");
       e.printStackTrace();
       }
       try
       {
       Connection DBcon = DriverManager.getConnection(url,userID,passwd);
       if(!DBcon.isClosed())
       out.println("成功的连接数据库");
       DBcon.close();
       }
       catch(SQLException SQLe)
       {
       out.println("无法连接数据库");
       }
       %>
    </body>
    </html>
      

  2.   

    对,没联网,只能用localhost了,你ping一下本机看看ip是就把它填上去就行
      

  3.   

    你可以利用数据源在JDBC中应用就可以了,你在控制面板中的工具那里添加系统数据源(这里你应该懂吧),然后利用这代码:
    <%@ page import="java.sql.*"%>
    <%
    //加载驱动
    String url=new String();
    url="jdbc:odbc:class(这里是你在控制面板odbc数据源定义的数据源名字)";Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    // Class.forName 装载驱动程序 
    Connection con=DriverManager.getConnection(url);
    //用适当的驱动程序类与 DBMS 建立一个连接
    Statement stmt=con.createStatement();
    //连接数据库%>  
    生成一个data.jsp就可以连接你的mysql服务器了。
      

  4.   

    mysql是通过管道连接。192.168.0.73 表示该数据源放在这台机器上,你的库在本级上,当然是127。0。0。1(localhost).
      

  5.   


    1、JDBC设置
    //登记JDBC驱动程序 
    Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //连接数据库 
    sqlConn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/xyw","xyw","1234"); //创建语句对象 
    sqlStmt = sqlConn.createStatement(); //获取来自客户端的数据 
    String ls_sql="select * from tbl_name"; 
    rs = sqlStmt.executeQuery(ls_sql);2、授予服务器小程序访问权
    mysql>grant all on xyw.tbl_name to xyw@localhost identified by '1234';