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>
解决方案 »
- ImageIcon是不是支持所有的图像格式哦?????????
- LinkedHashMap的负载因子干啥用的?
- 谁能提供emf-sdo-runtime-2.2.0.zip的有效连接或者Email给我?
- 帮帮我吧 一个关于TableModelListener的问题
- 你好,专家。我想问一个笔试常考题,望给给予答复。谢谢。
- 求助:如何用JAVA分析文档
- 请问输出怎么对齐
- 怎样得到InputStream流中数据块的大小
- 菜鸟问题
- Set接口、List、Map的差异
- Help!Help!关于在application中(不是applet)用MediaTracker读图片的问题,高分酬谢。
- [求助]找一份数据库连接池的代码(要求内详)
import java.util.*;
import java.sql.*;
import java.lang.*;public class DBConnect {
private String jdbcDriver=null;
private String jdbcURL=null;
private String userName=null;
private String password=null; public DBConnect() {
jdbcDriver="sun.jdbc.odbc.JdbcOdbcDriver";
jdbcURL="jdbc:odbc:cbk";
userName="system";
password="manager";
}
public Connection getConnection(){
Connection connection=null;
try{
Class.forName(jdbcDriver);
connection=DriverManager.getConnection(jdbcURL,userName,password);
}catch(Exception e){
System.out.println(e);
}
return connection;
}
}
Context ctx=new InitialContext()
ctx.lookup()
就可以了。
import java.sql.*; public class ConnectionPool implements Runnable
{
// Number of initial connections to make.
private int m_InitialConnectionCount = 5;
// A list of available connections for use.
private Vector m_AvailableConnections = new Vector();
// A list of connections being used currently.
private Vector m_UsedConnections = new Vector();
// The URL string used to connect to the database
private String m_URLString = null;
// The username used to connect to the database
private String m_UserName = null;
// The password used to connect to the database
private String m_Password = null;
// The cleanup thread
private Thread m_CleanupThread = null;
//Constructor
public ConnectionPool(String urlString, String user, String passwd) throws SQLException
{
// Initialize the required parameters
m_URLString = urlString;
m_UserName = user;
m_Password = passwd; for(int cnt=0; cnt<m_InitialConnectionCount; cnt++)
{
// Add a new connection to the available list.
m_AvailableConnections.addElement(getConnection());
}
// Create the cleanup thread
m_CleanupThread = new Thread(this);
m_CleanupThread.start();
}
private Connection getConnection() throws SQLException
{
return DriverManager.getConnection(m_URLString, m_UserName, m_Password);
}
public synchronized Connection checkout() throws SQLException
{
Connection newConnxn = null;
if(m_AvailableConnections.size() == 0)
{
// Im out of connections. Create one more.
newConnxn = getConnection();
// Add this connection to the "Used" list.
m_UsedConnections.addElement(newConnxn);
// We dont have to do anything else since this is
// a new connection.
}
else
{
// Connections exist !
// Get a connection object
newConnxn = (Connection)m_AvailableConnections.lastElement();
// Remove it from the available list.
m_AvailableConnections.removeElement(newConnxn);
// Add it to the used list.
m_UsedConnections.addElement(newConnxn);
}
// Either way, we should have a connection object now.
return newConnxn;
}
public synchronized void checkin(Connection c)
{
if(c != null)
{
// Remove from used list.
m_UsedConnections.removeElement(c);
// Add to the available list
m_AvailableConnections.addElement(c);
}
}
public int availableCount()
{
return m_AvailableConnections.size();
}
public void run()
{
try
{
while(true)
{
synchronized(this)
{
while(m_AvailableConnections.size() > m_InitialConnectionCount)
{
// Clean up extra available connections.
Connection c = (Connection)m_AvailableConnections.lastElement();
m_AvailableConnections.removeElement(c);
// Close the connection to the database.
c.close();
}
// Clean up is done
}
System.out.println("CLEANUP : Available Connections : " + availableCount());
// Now sleep for 1 minute
Thread.sleep(60000 * 1);
}
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
连接池的主要思想:
1。Requests for Connection
2.Unused connection moves to used list.
3.Connection Pool returns unused connection
4.After using connection,modules return them,the connection pool returns them to the unused pool.