JSP连接access
用数据源连接的
怎么改成JSP连接MYSQL的呢?连接数据库的类该怎么改呢?
用数据源连接的
怎么改成JSP连接MYSQL的呢?连接数据库的类该怎么改呢?
解决方案 »
- 线程的问题
- MyBatis 3 如何清理一级缓存
- ORACLE上哪里可以下载Application server 10g Release 3 (10.1.3) middle tiers
- 论坛发帖页面那种能既能上传文件又能提交表单的界面是怎么实现的
- 使用TOMCAT连接网站及数据库的问题 急!在线等,万分感谢.
- 在一个应用中,如何衡量哪些对象使用ejb
- 求教:EJB方法的参数问题
- 请问在JBuilder7中,如何引入javax.xml.parsers.*;org.xml.sax.*;org.w3c.dom.*;
- 求解J2EE.
- 关于struts2中token自身提交到自身时的小麻烦
- Generated servlet error:
- 关于EJB代理类中的静态变量使用问题?
具体那个串是什么样的我也记不住了,现在都是用直连的方式我觉得这个最好是找Google
是要在哪改呢
给你发个例子..找来的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)
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 用户名和密码
主要是改一下几个方面
1,改驱动包
2,连接驱动和数据库用户名等
3,修改你的个别的SQL语句(如果楼主使用的是标准SQL就忽略此条)
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的类 这个要怎么改呢
直连的话, 查 jdbc MySql,要多少有多少。