一.写入BLOB
1.先在blob中插入empty_blob()
2.获得对刚刚插入记录的引用
BLOB blob = (BLOB) rs.getBlob("你的blob字段名称");
3.写入
OutputStream out = blob.getBinaryOutputStream();
out.write(ENCYPWD);//注意q里
?dBLOB
1.blob = rs.getBlob("你的blob字段名称");
2.
InputStream is = blob.getBinaryStream();
int length = (int) blob.length();
byte[] buffer = new byte[length];
is.read(buffer);
is.close();
3.你有?jin)is随便处理了(jin)
比如说输出到一个文?br>FileOutputStream fo = new FileOutputStream(filename);//数据到的文g?br>fo.write(buffer);
fo.close();
String url=”jdbc:inetdae:myserver:1433?language=us-english&sql7=true? |
Class.forName(“?; |
Class.forName(“?.newInstance(); |
Connection connection=DriverManager.getConnection(url,login,password)Q? |
Statement stmt=connection.createStatement(); |
importjava.sql.*; // 输入JDBC package String url = "jdbc:inetdae:myserver:1433";// L?/a>和端? String login = "user";// d? String password = "";// 密码 try { DriverManager.setLogStream(System.out); file://为显CZ些的信息打开一个流 file://调用驱动E序Q其名字?a class="bluekey" target="_blank">com.inet.tds.TdsDriver file://Class.forName("com.inet.tds.TdsDriver")Q? file://讄时 DriverManager.setLoginTimeout(10); file://打开一个连? Connection connection = DriverManager.getConnection(url,login,password); file://得到数据库驱动程序版? DatabaseMetaData conMD = connection.getMetaData(); System.out.println("DriverName:\t" + conMD.getDriverName()); System.out.println("DriverVersion:\t" + conMD.getDriverVersion()); file://选择数据? connection.setCatalog( "MyDatabase"); file://创徏Statement Statement st = connection.createStatement(); file://执行查询 ResultSet rs = st.executeQuery("SELECT * FROM mytable"); file://取得l果Q输出到屏幕 while (rs.next()){ for(int j=1; j<=rs.getMetaData().getColumnCount(); j++){ System.out.print( rs.getObject(j)+"\t"); } System.out.println(); } file://关闭对象 st.close(); connection.close(); } catch(Exception e) { e.printStackTrace(); } |
public DBConnectionPool(String name, String URL, String user, String password, int maxConn) { this.name = name; this.URL = URL; this.user = user; this.password = password; this.maxConn = maxConn; } |
public synchronized Connection getConnection() { Connection con = null; if (freeConnections.size() > 0) { // Pick the first Connection in the Vector // to get round-robin usage con = (Connection) freeConnections.firstElement(); freeConnections.removeElementAt(0); try { if (con.isClosed()) { log("Removed bad connection from " + name); // Try again recursively con = getConnection(); } } catch (SQLException e) { log("Removed bad connection from " + name); // Try again recursively con = getConnection(); } } else if (maxConn == 0 || checkedOut < maxConn) { con = newConnection(); } if (con != null) { checkedOut++; } return con; } |
private Connection newConnection() { Connection con = null; try { if (user == null) { con = DriverManager.getConnection(URL); } else { con = DriverManager.getConnection(URL, user, password); } log("Created a new connection in pool " + name); } catch (SQLException e) { log(e, "Can not create a new connection for " + URL); return null; } return con; } |
public synchronized Connection getConnection(long timeout) { long startTime = new Date().getTime(); Connection con; while ((con = getConnection()) == null) { try { wait(timeout); } catch (InterruptedException e) {} if ((new Date().getTime() - startTime) >= timeout) { // Timeout has expired return null; } } return con; } |
public synchronized void freeConnection(Connection con) { // Put the connection at the end of the Vector freeConnections.addElement(con); checkedOut--; notifyAll(); } |
public synchronized void release() { Enumeration allConnections = freeConnections.elements(); while (allConnections.hasMoreElements()) { Connection con = (Connection) allConnections.nextElement(); try { con.close(); log("Closed connection for pool " + name); } catch (SQLException e) { log(e, "Can not close connection for pool " + name); } } freeConnections.removeAllElements(); } |
private DBConnectionManager() { init(); } |
static synchronized public DBConnectionManager getInstance() { if (instance == null) { instance = new DBConnectionManager(); } clients++; return instance; } |
private void init() { InputStream is = getClass().getResourceAsStream("/db.properties"); Properties dbProps = new Properties(); try { dbProps.load(is); } catch (Exception e) { System.err.println("Can not read the properties file. " + "Make sure db.properties is in the CLASSPATH"); return; } String logFile = dbProps.getProperty("logfile", "DBConnectionManager.log"); try { log = new PrintWriter(new FileWriter(logFile, true), true); } catch (IOException e) { System.err.println("Can not open the log file: " + logFile); log = new PrintWriter(System.err); } loadDrivers(dbProps); createPools(dbProps); } |
drivers=sun.jdbc.odbc.JdbcOdbcDriver jdbc.idbDriver logfile=D:\\user\\src\\java\\DBConnectionManager\\log.txt idb.url=jdbc:idb:c:\\local\\javawebserver1.1\\db\\db.prp idb.maxconn=2 access.url=jdbc:odbc:demo access.user=demo access.password=demopw |
private void loadDrivers(Properties props) { String driverClasses = props.getProperty("drivers"); StringTokenizer st = new StringTokenizer(driverClasses); while (st.hasMoreElements()) { String driverClassName = st.nextToken().trim(); try { Driver driver = (Driver) Class.forName(driverClassName).newInstance(); DriverManager.registerDriver(driver); drivers.addElement(driver); log("Registered JDBC driver " + driverClassName); } catch (Exception e) { log("Can not register JDBC driver: " + driverClassName + ", Exception: " + e); } } } |
private void createPools(Properties props) { Enumeration propNames = props.propertyNames(); while (propNames.hasMoreElements()) { String name = (String) propNames.nextElement(); if (name.endsWith(".url")) { String poolName = name.substring(0, name.lastIndexOf(".")); String url = props.getProperty(poolName + ".url"); if (url == null) { log("No URL specified for " + poolName); continue; } String user = props.getProperty(poolName + ".user"); String password = props.getProperty(poolName + ".password"); String maxconn = props.getProperty(poolName + ".maxconn", "0"); int max; try { max = Integer.valueOf(maxconn).intValue(); } catch (NumberFormatException e) { log("Invalid maxconn value " + maxconn + " for " + poolName); max = 0; } DBConnectionPool pool = new DBConnectionPool(poolName, url, user, password, max); pools.put(poolName, pool); log("Initialized pool " + poolName); } } } |
public Connection getConnection(String name) { DBConnectionPool pool = (DBConnectionPool) pools.get(name); if (pool != null) { return pool.getConnection(); } return null; } public Connection getConnection(String name, long time) { DBConnectionPool pool = (DBConnectionPool) pools.get(name); if (pool != null) { return pool.getConnection(time); } return null; } public void freeConnection(String name, Connection con) { DBConnectionPool pool = (DBConnectionPool) pools.get(name); if (pool != null) { pool.freeConnection(con); } } |
public synchronized void release() { // Wait until called by the last client if (--clients != 0) { return; } Enumeration allPools = pools.elements(); while (allPools.hasMoreElements()) { DBConnectionPool pool = (DBConnectionPool) allPools.nextElement(); pool.release(); } Enumeration allDrivers = drivers.elements(); while (allDrivers.hasMoreElements()) { Driver driver = (Driver) allDrivers.nextElement(); try { DriverManager.deregisterDriver(driver); log("Deregistered JDBC driver " + driver.getClass().getName()); } catch (SQLException e) { log(e, "Can not deregister JDBC driver: " + driver.getClass().getName()); } } } |
import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class TestServlet extends HttpServlet { private DBConnectionManager connMgr; public void init(ServletConfig conf) throws ServletException { super.init(conf); connMgr = DBConnectionManager.getInstance(); } public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); Connection con = connMgr.getConnection("idb"); if (con == null) { out.println("Cant get connection"); return; } ResultSet rs = null; ResultSetMetaData md = null; Statement stmt = null; try { stmt = con.createStatement(); rs = stmt.executeQuery("SELECT * FROM EMPLOYEE"); md = rs.getMetaData(); out.println("Employee data "); while (rs.next()) { out.println(" "); for (int i = 1; i < md.getColumnCount(); i++) { out.print(rs.getString(i) + ", "); } } stmt.close(); rs.close(); } catch (SQLException e) { e.printStackTrace(out); } connMgr.freeConnection("idb", con); } public void destroy() { connMgr.release(); super.destroy(); } } |