javascript
JSP实战型程序连载:通用数据库连接JavaBean
package online;
?
import java.sql.*;
?
public class DBConn {
? private static String rootpath = "web發布路徑";
? private String sample = "sample";
? private Connection con = null;
? private Statement stmt = null;
? ResultSet rs = null;
? /***************************************************************/
? private static final String DRIVE = "sun.jdbc.odbc.JdbcOdbcDriver";
? //暫時使用jdbc-odbc連接//"com.microsoft.jdbc.sqlserver.SQLServerDriver";
? private static final String USERNAME = "sa";
? private static final String PASSWORD = "123aaa";
? private static final String HOST = "http:127.0.0.1:8080/renshi";
? /*************************************************************/
? //暫時使用jdbc-odbc數據源
? private static final String CONNECTION_STRING = "jdbc:odbc:renshi";
? //"jdbc:microsoft:sqlserver://localhost;1433;";
? public static String getRootPath() {
??? return rootpath;
? }
?
? public DBConn() { //加載驅動
??? try {
????? Class.forName(DRIVE);
??? }
??? catch (ClassNotFoundException e) {
????? System.err.println("DBConn():" + e.toString());
??? }
??? catch (Exception e) {
????? System.err.println("DBConn():" + e.toString());
??? }
? }
?
? public Connection getConnection() { //得到連接
??? try {
????? String strUrl = CONNECTION_STRING;
????? /***********周五晚改動****************************************/
????? //+ "DatebaseName=renshi," + USERNAME +"," + PASSWORD;
????? con = DriverManager.getConnection(strUrl, this.USERNAME, this.PASSWORD);
??? }
??? catch (Exception e) {
????? con = null;
??? }
??? return con;
? }
?
? public void dropConnection() { //關閉連接
??? try {
????? closeStmt();
????? con.close();
??? }
??? catch (Exception ignored) {
??? }
??? finally {
????? con = null;
??? }
? }
?
? public ResultSet executeQuery(String sql) { //執行sql查詢
??? ResultSet rs = null;
??? try {
????? con = getConnection();
????? stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
???????????????????????????????? ResultSet.CONCUR_READ_ONLY);
????? rs = stmt.executeQuery(sql);
??? }
??? catch (SQLException ex) {
????? System.err.println("DBConn.executeQuery():" + ex.getMessage());
??? }
??? return rs;
? }
?
? public int executeUpdate(String sql) { //執行sql更新語句
??? int i=0;
??? stmt = null;
??? rs = null;
??? try {
????? con = getConnection();
????? stmt = con.createStatement();
???? i= stmt.executeUpdate(sql);
????? stmt.close();
????? con.close();
??? }
??? catch (SQLException ex) {
????? System.err.println("DBConn:executeUpdate(0:" + ex.getMessage());
??? }
??? return i;
? }
?
? public void execute(String sql) { //執行sql語句
??? stmt = null;
??? rs = null;
??? try {
????? con = getConnection();
????? stmt = con.createStatement();
????? stmt.execute(sql);
????? stmt.close();
????? con.close();
??? }
??? catch (SQLException ex) {
????? System.err.println("DBConn:excute():" + ex.getMessage());
??? }
? }
?
? public void closeConn() { //關閉sql連接
??? try {
????? stmt.close();
??? }
??? catch (SQLException e) {
????? e.printStackTrace();
??? }
? }
?
? public void closeStmt() { //關閉sql連接
??? try {
????? con.close();
??? }
??? catch (SQLException e) {
????? e.printStackTrace();
??? }
?
? }
?
? /**
?? * main
?? */
? public static void main(String[] args) throws SQLException {
??? DBConn one=new DBConn();
??? ResultSet rs=one.executeQuery("select * from PS_INFO");
??? while(rs.next()){
????? System.out.println(rs.getString(2));
??? }
? }
?
}
?
轉載于:https://www.cnblogs.com/duadu/archive/2006/05/30/6167173.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的JSP实战型程序连载:通用数据库连接JavaBean的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring boot中servlet启
- 下一篇: gradle idea java ssm