涓.鍐欏叆BLOB
1.鍏堝湪blob涓彃鍏mpty_blob()
2.鑾峰緱瀵瑰垰鍒氭彃鍏ヨ褰曠殑寮曠敤
BLOB blob = (BLOB) rs.getBlob("浣犵殑blob瀛楁鍚嶇О");
3.鍐欏叆
OutputStream out = blob.getBinaryOutputStream();
out.write(ENCYPWD);//娉ㄦ剰榪欓噷
浜?璇誨嚭BLOB
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.浣犳湁浜唅s灝遍殢渚垮鐞嗕簡
姣斿璇磋緭鍑哄埌涓涓枃浠?br>FileOutputStream fo = new FileOutputStream(filename);//鏁版嵁鍒扮殑鏂囦歡鍚?br>fo.write(buffer);
fo.close();
String url=鈥漥dbc:inetdae:myserver:1433?language=us-english&sql7=true鈥? |
Class.forName(鈥溾?; |
Class.forName(鈥溾?.newInstance(); |
Connection connection=DriverManager.getConnection(url,login,password)錛? |
Statement stmt=connection.createStatement(); |
importjava.sql.*; // 杈撳叆JDBC package String url = "jdbc:inetdae:myserver:1433";// 涓繪満鍚?/a>鍜岀鍙? String login = "user";// 鐧誨綍鍚? String password = "";// 瀵嗙爜 try { 銆銆DriverManager.setLogStream(System.out); file://涓烘樉紺轟竴浜涚殑淇℃伅鎵撳紑涓涓祦 銆銆file://璋冪敤椹卞姩紼嬪簭錛屽叾鍚嶅瓧涓?a class="bluekey" target="_blank">com.inet.tds.TdsDriver 銆銆file://Class.forName("com.inet.tds.TdsDriver")錛? 銆銆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://鍙栧緱緇撴灉錛岃緭鍑哄埌灞忓箷 銆銆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(); 銆} } |