String dbDriverName = "sun.jdbc.odbc.JdbcOdbcDriver";
// Connection URL.
String dbConnectionURL = "jdbc:odbc:test";
// Connection object.
Connection dbConnection = null;
// Statement object.
Statement dbStatement = null;
// SQL statement to execute.
String sqlStatement = "Select * from oneTable";
// Result set object.
ResultSet dbResultSet = null; // Start the servlet output.
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<html><head><title>DB Test</title></head><body>");
out.println("<h1>Database Test</h1>");
// Create JDBC driver instance.
Class.forName(dbDriverName).newInstance();
dbConnection = DriverManager.getConnection(dbConnectionURL);
// Create Statement object.
dbStatement = dbConnection.createStatement();
// Execute the query.
dbResultSet = dbStatement.executeQuery(sqlStatement);
// Connection URL.
String dbConnectionURL = "jdbc:odbc:test";
// Connection object.
Connection dbConnection = null;
// Statement object.
Statement dbStatement = null;
// SQL statement to execute.
String sqlStatement = "Select * from oneTable";
// Result set object.
ResultSet dbResultSet = null; // Start the servlet output.
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<html><head><title>DB Test</title></head><body>");
out.println("<h1>Database Test</h1>");
// Create JDBC driver instance.
Class.forName(dbDriverName).newInstance();
dbConnection = DriverManager.getConnection(dbConnectionURL);
// Create Statement object.
dbStatement = dbConnection.createStatement();
// Execute the query.
dbResultSet = dbStatement.executeQuery(sqlStatement);
解决方案 »
- http://localhost:8080无法显示
- 在JS中创建一个输出流的对象,知道的进~~
- JSTL标签${products}为什么不显示内容?
- Action的javabean被压到前台用webwork显示后,再扔给后台,未显示的值变空了。
- tomcat怎么关闭报错功能?
- 请教关于.do网页的参数传递问题?
- 发自己的毕业设计程序 JSP的新闻发布系统 顺便想在北京找份活
- 解析xml错误。no protocol错误。
- 初级问题:jsp服务器一般用哪种?linux下jsp可以和另一台sql server连接吗?
- java a标签下载tomcat中文件,jpg,png等好多都会直接打开
- 请教:system.out.println("a="+a),a是int类型的,那个+号到底起什么作用呢?
- Error 500 在JSP页面中出现的原因到底是什么?java代码不规范?
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
然后在按楼上得楼上的楼上的楼上试试!!!!!!!!!
//将下面的名为Conn的Bean封装在名为dbms包中
package DBMS;
//导入java.sql包中的类
import java.sql.*;
public class Conn{
private String _username; //数据库的用户名
private String _password; //该用户对应的密码
private String _driver; //JDBC连接数据库的驱动程序名
private String _dburl; //数据库的URL
private Connection _con;
private Statement _stmt;
//设置初始化,默认设置
public Conn(){
_dburl = "jdbc:odbc:power";
_username = "12";
_password = "sadf.kk3";
_driver = "sun.jdbc.odbc.JdbcOdbcDriver";
}
//手动设置初始化
public Conn(String driver,String dburl,String username,String password){
_driver = driver;
_dburl = dburl;
_username = username;
_password = password;
}
//获得数据集
public ResultSet getDbResultSet(String sql){
ResultSet rset=null;
try{
Class.forName(_driver);
_con = DriverManager.getConnection(_dburl,_username,_password);
_stmt = _con.createStatement();
rset = _stmt.executeQuery(sql);
}catch(Exception e){
System.out.println("查询中读取数据库错误:"+e);
}
return rset;
}
//修改、删除、插入
public int updateDb(String sql){
int flag=0;
try{
Class.forName(_driver);
_con = DriverManager.getConnection(_dburl,_username,_password);
_stmt = _con.createStatement();
flag = _stmt.executeUpdate(sql);
}catch(Exception e){
System.out.println("更新中数据库操作错误:"+e);
}
return flag;
}}
<%@ page language="java" import="DBMS.Conn,java.sql.*"%>
<%@ page contentType="text/html;charset=gb2312" %>
<jsp:useBean id="clg" class="DBMS.Conn" scope="session"/>
<%
Resultset rs=clg.getDbResultSet("select * from bbs");
%>
现导入:
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>结果连接数据库ok.
是不是我的环境配置还是有问题,各位做数据库连接时是否也导入上述类包?