JDBC工具类
- 格式:doc
- 大小:68.50 KB
- 文档页数:13
五、jsp连接Sybase数据库? 塪 三S testmysql.jsp如下: 齅?5<%@ page contentType="text/html;charset=gb2312"%> ? ?孶縦<%@ page import="java.sql.*"%> +肛Yl蠄 狒<html> 陫5 Ady?<body> ]Ci=?乷b?<%Class.forName("com.sybase.jdbc.SybDriver").newInstance(); R ?髰?String url =" jdbc:sybase:Tds:localhost:5007/tsdata"; P払? ?//tsdata为你的数据库名 .3dVvU粼Properties sysProps = System.getProperties(); ?) _昫龇SysProps.put("user","userid"); 轾HH. 芴竑SysProps.put("password","user_password"); `Y >矃jK1Connection conn= Dr iverManager.getConnection(url, SysProps); S-咢6s菘Statementstmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR _UPDATABLE); iE眹 %陀String sql="select * from test"; ?lt;拦 岛董ResultSet rs=stmt.executeQuery(sql); \鹍 #禺?:while(rs.next()) {%> +x?A???您的第一个字段内容为:<%=rs.getString(1)%> 奃h#}坜您的第二个字段内容为:<%=rs.getString(2)%> 敼蠵2Ⅱ?<%}%> R綮?铞邩?<%out.print("数据库操作成功,恭喜你");%> ??霬 ,<%rs.close(); 记?憍J髦?stmt.close(); 瞦样\芢?conn.close(); l洑`T ?%> hlb?誊?</body> 赡?肢J??</html> 夙蜵l卟UT?import java.util.*;import java.io.*;public class PropertyManager {private Properties properties = new Properties();File file;/** Creates a new instance of PropertyManager */public PropertyManager(String fileName) {String path = getClass().getResource("/").getPath();try{try{file = new File(path,fileName);}catch(Exception e){file = new File(path,"/"+fileName);}FileInputStream fis = new FileInputStream(file);properties.load(fis);fis.close();}catch(Exception e){e.printStackTrace();}}public String get(String key){return properties.getProperty(key.trim());}public String get(String key,String defaultValue){return properties.getProperty(key.trim(), defaultValue);}public void set(String key,String value){try{properties.setProperty(key,value);//new String(value.getBytes("UTF-8"), "UTF-8") }catch(Exception e){properties.setProperty(key, value);e.printStackTrace();}}synchronized public boolean store(){String path = getClass().getResource("/").getPath();boolean b = false;try{FileOutputStream fos = new FileOutputStream(file);properties.store(fos,"");fos.close();b = true;}catch(Exception e){b = false;e.printStackTrace();}file = null;return b;}public static void main(String arg[]){String path = new Object().getClass().getResource("/").getPath();System.out.println(path);}}import java.util.*;import java.io.*;public class PropertyManager {private Properties properties = new Properties();File file;/** Creates a new instance of PropertyManager */public PropertyManager(String fileName) {String path = getClass().getResource("/").getPath();try{try{file = new File(path,fileName);}catch(Exception e){file = new File(path,"/"+fileName);}FileInputStream fis = new FileInputStream(file);properties.load(fis);fis.close();}catch(Exception e){e.printStackTrace();}}public String get(String key){return properties.getProperty(key.trim());}public String get(String key,String defaultValue){return properties.getProperty(key.trim(), defaultValue);}public void set(String key,String value){try{properties.setProperty(key,value);//new String(value.getBytes("UTF-8"), "UTF-8") }catch(Exception e){properties.setProperty(key, value);e.printStackTrace();}}synchronized public boolean store(){String path = getClass().getResource("/").getPath();boolean b = false;try{FileOutputStream fos = new FileOutputStream(file);properties.store(fos,"");fos.close();b = true;}catch(Exception e){b = false;e.printStackTrace();}file = null;return b;}public static void main(String arg[]){String path = new Object().getClass().getResource("/").getPath();System.out.println(path);}}此类为读取*.properties的工具类Java代码public class DataBassInfo {private String driver;private String url;private String userName;private String password;/** Creates a new instance of DataBassInfo */public DataBassInfo() {PropertyManager pm = new PropertyManager("system.properties");driver = pm.get("DB_DRIVER");url = pm.get("DB_URL");userName = pm.get("DB_USERNAME");password = pm.get("DB_PASSWORD");}public String getDriver() {return driver;}public void setDriver(String driver) {this.driver = driver;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getUserName() {return userName;}public void setUserName(String userName) { erName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}public class DataBassInfo {private String driver;private String url;private String userName;private String password;/** Creates a new instance of DataBassInfo */public DataBassInfo() {PropertyManager pm = new PropertyManager("system.properties");driver = pm.get("DB_DRIVER");url = pm.get("DB_URL");userName = pm.get("DB_USERNAME");password = pm.get("DB_PASSWORD");}public String getDriver() {return driver;}public void setDriver(String driver) {this.driver = driver;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getUserName() {return userName;}public void setUserName(String userName) {erName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}此类为读取*.properties内容的bean类Java代码import java.sql.*;import java.util.ArrayList;public class ConnectionPool {protected static ConnectionPool connectionPool;protected String DBdriver;protected String DBurl;protected String DBuserName;protected String DBpassword;protected int maxConnection;protected int conNumber;protected ArrayList freeCon;/** Creates a new instance of ConnectionPool */protected ConnectionPool(DataBassInfo dbi){conNumber = 0;DBdriver = dbi.getDriver(); //"com.mysql.jdbc.Driver";DBurl = dbi.getUrl(); //"jdbc:mysql://localhost:3306/luru?autoReconnect=true&useUnicode=true&characterEncoding= UTF-8";DBuserName = dbi.getUserName(); //"";DBpassword = dbi.getPassword(); //"";freeCon = new ArrayList();try {Class.forName(DBdriver);} catch (ClassNotFoundException ex) {ex.printStackTrace();}System.out.println(DBurl);}public synchronized Connection getConnection(){Connection con = null;try {if(!freeCon.isEmpty()){con = (Connection)freeCon.get(0);freeCon.remove(0);}else{if(maxConnection == 0 || conNumber<maxConnection){con = DriverManager.getConnection(DBurl,DBuserName,DBpassword);conNumber++;}else{wait(2000);return getConnection();}}if(con!=null&&con.isClosed()){conNumber--;return getConnection();}} catch (Exception ex) {System.out.println("CONNECT IS NULL");con = null;ex.printStackTrace();}return con;}public synchronized void restore(Connection con){if(con!=null){try{freeCon.add(con);notifyAll();}catch (Exception ex) {con = null;ex.printStackTrace();}}}public synchronized void closeAll(){while(!freeCon.isEmpty()){Connection con = (Connection)freeCon.get(0);freeCon.remove(0);try {con.close();conNumber--;} catch (SQLException ex) {ex.printStackTrace();}}}public void setMaxConnection(int maxConnection){this.maxConnection = maxConnection;}public static ConnectionPool getInstance(){return getInstance(20);}public static synchronized ConnectionPool getInstance(int maxConnection){ if(connectionPool == null){DataBassInfo dbi = new DataBassInfo();connectionPool = new ConnectionPool(dbi);connectionPool.setMaxConnection(maxConnection);}return connectionPool;}protected void finalize() throws Throwable{closeAll();}}import java.sql.*;import java.util.ArrayList;public class ConnectionPool {protected static ConnectionPool connectionPool;protected String DBdriver;protected String DBurl;protected String DBuserName;protected String DBpassword;protected int maxConnection;protected int conNumber;protected ArrayList freeCon;/** Creates a new instance of ConnectionPool */protected ConnectionPool(DataBassInfo dbi){conNumber = 0;DBdriver = dbi.getDriver(); //"com.mysql.jdbc.Driver";DBurl = dbi.getUrl(); //"jdbc:mysql://localhost:3306/luru?autoReconnect=true&useUnicode=true&characterEncoding= UTF-8";DBuserName = dbi.getUserName(); //"";DBpassword = dbi.getPassword(); //"";freeCon = new ArrayList();try {Class.forName(DBdriver);} catch (ClassNotFoundException ex) {ex.printStackTrace();}System.out.println(DBurl);}public synchronized Connection getConnection(){Connection con = null;try {if(!freeCon.isEmpty()){con = (Connection)freeCon.get(0);freeCon.remove(0);}else{if(maxConnection == 0 || conNumber<maxConnection){con = DriverManager.getConnection(DBurl,DBuserName,DBpassword);conNumber++;}else{wait(2000);return getConnection();}}if(con!=null&&con.isClosed()){conNumber--;return getConnection();}} catch (Exception ex) {System.out.println("CONNECT IS NULL");con = null;ex.printStackTrace();}return con;}public synchronized void restore(Connection con){if(con!=null){try{freeCon.add(con);notifyAll();}catch (Exception ex) {con = null;ex.printStackTrace();}}}public synchronized void closeAll(){while(!freeCon.isEmpty()){Connection con = (Connection)freeCon.get(0);freeCon.remove(0);try {con.close();conNumber--;} catch (SQLException ex) {ex.printStackTrace();}}}public void setMaxConnection(int maxConnection){this.maxConnection = maxConnection;}public static ConnectionPool getInstance(){return getInstance(20);}public static synchronized ConnectionPool getInstance(int maxConnection){ if(connectionPool == null){DataBassInfo dbi = new DataBassInfo();connectionPool = new ConnectionPool(dbi);connectionPool.setMaxConnection(maxConnection);}return connectionPool;}protected void finalize() throws Throwable{closeAll();}}此类位处理连接的工具类system.properties中的配置DB_USERNAME=DB_DRIVER=DB_PASSWORD=DB_URL=四个属性对应DataBassInfo中的使用即为调用ConnectionPool .getInstance()方法得到连接,然后就可以操作了。