jsp连接Sybase数据库
testmysql.jsp如下: <%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/tsdata";
//tsdata为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
testmysql.jsp如下: <%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/tsdata";
//tsdata为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
解决方案 »
- 关于tomcat7嵌入的几个问题
- 求助一个疑问
- servlet+jsp+javaBean
- 高分求助apache的fileupload问题
- 关于DATE型数据转换
- 小问题大毛病 --关于如何调用BEAN的方法 大家帮忙 分不够再加
- 求O'Reilly 的Java Servlet Programming, 2nd Edition英文版电子书
- 高分求解,在线等待!如何将Jsp页面的内容转换成word文档打印出来或像word一样打印出来?
- 我按照网上说的步骤,进入官网下载了spring包,但是,找了好久,没找到依赖包,它藏在哪里呀?大家能帮我找找吗,求具体步骤?
- spring报的这个错误应该怎么解决
- jsp中javabean的引用(分不多了,大家帮帮忙)
- 请教:如何将HTML表单转成WORD文档?
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/tsdata";
给个jdbc有什么用啊
贴出2个java文件代码:
//================= ConnectionBean.java ===================
import java.io.Serializable;
import java.sql.*;
public class ConnectionBean implements java.io.Serializable
{
private Connection connection = null;
private boolean inuse = false;
public ConnectionBean()
{ }
public ConnectionBean(Connection value)
{ if (value!=null) connection = value; }
public Connection getConnection()
{ return connection; }
public void setConnection(Connection con)
{ connection = con; }
public void setInuse(boolean value)
{ inuse = value; }
public boolean getInuse()
{ return inuse; }
public boolean inUse()
{ return inuse; }
public void close()
{
try
{ connection.close(); }
catch (SQLException sqle)
{ System.err.println(sqle.getMessage()); }
}
}
import java.io.Serializable;
import java.sql.*;
import java.util.*;
public class PoolBean implements java.io.Serializable
{
private String driver = null;
private String url = null;
private int size = 0;
private String username = new String("");
private String password = new String("");
private Vector pool = null;
public PoolBean() { }
public void setDriver(String value)
{ if (value!=null) driver=value; }
public String getDriver()
{ return driver; }
public void setURL(String value )
{ if (value!=null) url=value; }
public String getURL()
{ return url; }
public void setSize(int value)
{ if (value>1) size=value; }
public int getSize()
{ return size; }
public void setUsername(String value)
{ if (value!=null) username=value; }
public String getUserName()
{ return username; }
public void setPassword(String value)
{ if (value!=null) password=value; }
public String getPassword()
{ return password; }
private Connection createConnection() throws Exception
{
Connection con = null;
con = DriverManager.getConnection(url,username,password);
return con;
}
public synchronized void initializePool() throws Exception
{
if (driver==null)
throw new Exception("No Driver Name Specified!");
if (url==null)
throw new Exception("No URL Specified!");
if (size<1)
throw new Exception("Pool size is less than 1!");
try
{
Class.forName(driver);
for (int x=0; x<size; x++)
{
Connection con = createConnection();
if (con!=null)
{
ConnectionBean pcon = new ConnectionBean(con);
addConnection(pcon);
}
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
}
private void addConnection(ConnectionBean value)
{
if (pool==null) pool=new Vector(size);
pool.addElement(value);
}
public synchronized void releaseConnection(Connection con)
{
for (int x=0; x<pool.size(); x++)
{
ConnectionBean pcon =
(ConnectionBean)pool.elementAt(x);
if (pcon.getConnection()==con)
{
System.err.println("Releasing Connection " + x);
pcon.setInUse(false);
break;
}
}
}
public synchronized Connection getConnection()
throws Exception
{
ConnectionBean pcon = null;
for (int x=0; x<pool.size(); x++)
{
pcon = (ConnectionBean)pool.elementAt(x);
if (pcon.inUse()==false)
{
pcon.setInUse(true);
return pcon.getConnection();
}
}
try
{
Connection con = createConnection();
pcon = new ConnectionBean(con);
pcon.setInUse(true);
pool.addElement(pcon);
}
catch (Exception e)
{
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
return pcon.getConnection();
}
public synchronized void emptyPool()
{
for (int x=0; x<pool.size(); x++)
{
System.err.println("Closing JDBC Connection " + x);
ConnectionBean pcon =
(ConnectionBean)pool.elementAt(x);
if (pcon.inUse()==false)
pcon.close();
else
{
try
{
java.lang.Thread.sleep(30000);
pcon.close();
}
catch (InterruptedException ie)
{
System.err.println(ie.getMessage());
}
}
}
}
}
这个是自己写的吧,,,我很想在server.xml 和web.xml 里配置的,公司这样要求我做,,,,我配了很就都不行,,另外:nyliuhaiyang(lhy) 我这里有一个是适合你的:http://www.zdnet.com.cn/developer/code/story/0,2000081534,39134180,00.htm我参考的是上面那个和http://www.csdn.net/Develop/article/25/25942.shtm但都不行,我的数据库是sydase的,,,,各位那个会的帮忙一下,很急用