`
zhieer
  • 浏览: 40935 次
  • 性别: Icon_minigender_1
  • 来自: 南通
最近访客 更多访客>>
社区版块
存档分类
最新评论

粗心的代价

阅读更多
Class.forName("org.gjt.mm.mysql.Driver").newInstance();

import java.sql.*;

public class MysqlJdbc {
	private String url = "jdbc:mysql://localhost:3306/lunchclub";
	private Connection conn = null;
	private ResultSet rs = null;
	private Statement stmt = null;
	private PreparedStatement pstmt = null;
	
	public MysqlJdbc{
                  try{
                           Class.forName("org.gjt.mm.mysql.Driver").newInstance();
                           conn = DriverManager.getConnection(url,"root","abc123");
                     }catch(Exception eb){
			eb.printStackTrace();
		   }

         }
	
	public ResultSet getRs(String sql){
		try{
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			
		}catch(Exception eb){
			eb.printStackTrace();
		}
		return rs;
	}
	
	public PreparedStatement getPstmt(String sql){
		try{
			pstmt = conn.prepareStatement(sql);
		}catch(Exception ec){
			ec.printStackTrace();
		}
		return pstmt;
	}
	
	public void closeRs(){
		try{
			rs.close();
			stmt.close();
			conn.close();
		}catch(Exception ed){
			ed.printStackTrace();
		}
	}
	
	public void closePstmt(){
		try{
			pstmt.close();
			conn.close();
		}catch(Exception ef){
			ef.printStackTrace();
		}
	}

}
分享到:
评论
2 楼 zhieer 2006-10-10  
这个是主要是跑例子,其中那个驱动因为错了一个字母,害我吓忙了办天。
1 楼 geszJava 2006-10-10  
不用连接池的话,使用PreparedStatement好像无法缓存的.

相关推荐

Global site tag (gtag.js) - Google Analytics