去google搜索一下,另外csdn的文档里有很多关于这个方面的
解决方案 »
- 定义远程接口,能不能得到mysql数据源的JNDI?
- jsp获取MAC地址问题
- 在线商城管理系统的问题
- window.open()失效
- 求一个Java调用Weblogic10 JNDI的例子!
- myeclipse中log4j配置问题
- hibernate anotation 版本兼容问题
- MyEclipse10+axis2 生成客户端代码,运行时出现Wrong number of arguments!
- servlet程序部署到tomcat上为什么总是连接显示404找不到资源
- log4j设置自定义变量的问题
- DatagramSocket问题请教:No buffer space available异常
- EJB中从EJBLocalHome和EJBLocalObject继承下来类有什么用啊?
首先把三个*.jar文件放到了tomcat\common\lib的目录下.<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<div align="left">
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://super-happy:1433;DatabaseName=test";
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement statement = conn.createStatement();
String sql="select * from test";
ResultSet rs=statement.executeQuery(sql);
while(rs.next()) {%>
no1:<%=rs.getString(1)%><br>
no2:<%=rs.getString(2)%><br>
no3:<%=rs.getString(3)%><br>
no4:<%=rs.getString(4)%><br>
no5:<%=rs.getString(5)%><p>
<p>
<%}%>
<%out.print("数据库操作成功,开心死了");%>
<%rs.close(); %>
<%statement.close(); %>
<%conn.close(); %>
</p>
<p> </p>
</div>
</body>
</html>
import java.sql.*;
import java.io.*;
import java.util.Properties;public class SqlBean{ private String connDriver =null;
private String dbUrl =null;//数据库的地址和端口号
private Connection myConn = null;
private Statement stmt = null;
private ResultSet rs = null;
private String user = null;
private String pass = null; public SqlBean(){
}
public SqlBean(String Driver,String DbUrl,String User,String Pass){
connDriver = Driver;
dbUrl = DbUrl;
user = User;
pass = Pass;
}
public void makeConn() throws Exception{//完成连接数据库的功能
if(connDriver==null)
connDriver = getProperties("Driver");
if(dbUrl==null)
dbUrl = getProperties("dbUrl");
if(user==null)
user = getProperties("user");
if(pass==null)
pass = getProperties("pass");
Class.forName(connDriver);//找oracle的驱动
myConn=DriverManager.getConnection(dbUrl,user,pass); //连接数据库
}
public boolean sqlQuery(String sql) throws Exception{//执行select语句
stmt=myConn.createStatement();
rs=stmt.executeQuery(sql);
return (rs!=null);
}
public boolean getNext() throws Exception{//看数据库的下一个数据是否为空
return (rs.next());
}
public String getString(String inCol) throws Exception{//取得对应数据库字段的数据
return rs.getString(inCol);
}
public int getInt(String inCol) throws Exception{//取得对应数据库整型字段的数据
return rs.getInt(inCol);
}
public double getDouble(String inCol) throws Exception{//取得对应数据库整型字段的数据double
return rs.getDouble(inCol);
}
public void sqlUpdate(String sql) throws Exception{//执行update、delete、insert的sql语句
stmt=null;
stmt=myConn.createStatement();
stmt.executeUpdate(sql);
} public void closeAll() throws Exception{
if(rs!=null)
rs.close();
if(stmt != null)
stmt.close();
if(myConn != null)
myConn.close();
}
public void closeConn() throws Exception{//关闭所有的与数据库相关的操作
if(stmt != null)
stmt.close();
if(myConn != null)
myConn.close();
}
public void closeRs() throws Exception{
if(rs != null)
rs.close();
}
public void closeStmt() throws Exception{
if(stmt != null)
stmt.close();
}
public void closeConnect() throws Exception{
if(myConn != null)
myConn.close();
}
public String getProperties(String Pname) throws Exception{
String dbUser = null;
String fileName = "/config/POIdb.properties";//POIdb.properties不数据库配置文件
InputStream inputstream=getClass().getResourceAsStream(fileName);
/*fd.read();
BufferedReader br = new BufferedReader(fd);
dbUser = br.readLine();
fd.close();
br.close();*/
Properties properties = new Properties();
properties.load(inputstream);
dbUser = properties.getProperty(Pname);
return dbUser;
}
}在JSP页面中,只要定义一个实例或定义一个JAVABEAN就可以了.