JSP连接access  
用数据源连接的
怎么改成JSP连接MYSQL的呢?连接数据库的类该怎么改呢?

解决方案 »

  1.   

    如果用数据源还是要先配置吧
    具体那个串是什么样的我也记不住了,现在都是用直连的方式我觉得这个最好是找Google
      

  2.   

    呃 连接access是用数据源的现在要改成连接MYSQL的
    是要在哪改呢
      

  3.   

    是TOMCAT吗?在server.xml里改吧
    给你发个例子..找来的1)在server.xml中配置数据源       <Context path="/jndi" docBase="D:\Documents and Settings\sunxf\workspace\jndi" >  
    <Resource name="jdbc/myoracle" auth="Container" type="javax.sql.DataSource"/>  
    <ResourceParams name="jdbc/myoracle">  
    <parameter>  
    <name>username</name>  
    <value>sunxf</value>  
    </parameter>  
    <parameter>  
    <name>password</name>  
    <value>sunxf</value>  
    </parameter>  
    <parameter>  
    <name>driverClassName</name>  
    <value>oracle.jdbc.driver.OracleDriver</value>  
    </parameter>  
    <parameter>  
    <name>url</name>  
    <value>jdbc:oracle:thin:@127.0.0.1:1521:SUNXF</value>  
    </parameter>  
    </ResourceParams>  
    </Context>  
    2)在web.xml中配置:      <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><description>Oracle Datasource example</description>
    <res-ref-name>jdbc/myoracle</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
    </web-app> 
    3)编写JSP测试:<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <%@ page import="javax.naming.*,javax.sql.DataSource, java.sql.*" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'javabeansource.jsp' starting page</title>
        
     <meta http-equiv="pragma" content="no-cache">
     <meta http-equiv="cache-control" content="no-cache">
     <meta http-equiv="expires" content="0">    
     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
     <meta http-equiv="description" content="This is my page">
     <!--
     <link rel="stylesheet" type="text/css" href="styles.css">
     -->  </head>
      
      <body>
        <%Context initContext = new InitialContext();
     Context envContext  = (Context)initContext.lookup("java:/comp/env");
     DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
     Connection conn = ds.getConnection();
        out.println("ok"); 
        Statement stmt = conn.createStatement(); 
        ResultSet rs = stmt.executeQuery("select id from t1"); 
        while(rs.next()){
        out.println(rs.getInt(1));};
        %>
      </body>
    </html>
    4)结果:  http://localhost:8088/jndi/WebRoot/javabeansource.jspok 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808文章出处:DIY部落(http://www.diybl.com/course/4_webprogram/jsp/jsp_js/2008919/143433.html)
      

  4.   

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;public class DBCon {
    public static String driver="oracle.jdbc.driver.OracleDriver";
    public static String url="jdbc:oracle:thin:@192.168.0.174:1521:ORCL";
    public static String user="scott";
    public static String pwd="tiger";

    public static Connection getConnection(){
    try {
    Class.forName(driver);
    Connection con=DriverManager.getConnection(url, user, pwd);
    System.out.println("con success");
    return con;
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null;
    }

    public static void close(Connection con,Statement stm,ResultSet rs){
    if(con!=null){
    try {
    con.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(stm!=null){
    try {
    stm.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(rs!=null){
    try {
    rs.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    public static void main(String[] args) {
    DBCon.getConnection();
    }
    }
    换驱动 url 用户名和密码
      

  5.   

      关键是楼主你是怎么连接access的帖一下代码就清楚了
        主要是改一下几个方面
        1,改驱动包
        2,连接驱动和数据库用户名等
        3,修改你的个别的SQL语句(如果楼主使用的是标准SQL就忽略此条)
      

  6.   

    package ckstudio.db;import java.io.PrintStream;
    import java.sql.*;public class faq
    {
        String      sDBDriver;
        String      sConnStr;
        Connection  conn;
        ResultSet   rs;    public faq()
        {
            sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
            sConnStr = "jdbc:odbc:zsmshop";  //设置数据源 zsmshop
            conn = null;
            rs = null;
            try
            {
                Class.forName(sDBDriver);
            }
            catch(ClassNotFoundException classnotfoundexception)
            {
                System.err.println("faq(): " + classnotfoundexception.getMessage());
            }
        }    public void executeInsert(String s)
        {
            try
            {
                conn = DriverManager.getConnection(sConnStr);
                Statement statement = conn.createStatement();
                int i = statement.executeUpdate(s);
            }
            catch(SQLException sqlexception)
            {
                System.err.println("faq.executeUpdate:" + sqlexception.getMessage());
            }
        }    public ResultSet executeQuery(String s)
        {
            rs = null;
            try
            {
                conn = DriverManager.getConnection(sConnStr);
                Statement statement = conn.createStatement(1004, 1008);
                rs = statement.executeQuery(s);
            }
            catch(SQLException sqlexception)
            {
                System.err.println("aq.executeQuery: " + sqlexception.getMessage());
            }
            return rs;
        }    public void executeDelete(String s)
        {
            try
            {
                conn = DriverManager.getConnection(sConnStr);
                Statement statement = conn.createStatement();
                statement.executeUpdate(s);
            }
            catch(SQLException sqlexception)
            {
                System.err.println("faq.executeDelete: " + sqlexception.getMessage());
            }
        }    public int executeUpdate(String s)
        {
            int i = 0;
            try
            {
                conn = DriverManager.getConnection(sConnStr);
                Statement statement = conn.createStatement();
                i = statement.executeUpdate(s);
            }
            catch(SQLException sqlexception)
            {
                System.err.println("faq.executeDelete: " + sqlexception.getMessage());
            }
            return i;
        }    public void Close()
        {
            try
            {
                if(rs != null)
                    rs.close();
                if(conn != null)
                    conn.close();
            }
            catch(SQLException sqlexception)
            {
                System.err.println("faq.executeDelete: " + sqlexception.getMessage());
            }
        }}
    连接ACCESS的类  这个要怎么改呢
      

  7.   

    ODBC数据源中建个zsmshop1连到mysql,然后这里zsmshop改为zsmshop1,用户名密码相应变下。
    直连的话, 查 jdbc MySql,要多少有多少。