---
title: "粗心的代价"
author: "zhieer"
date: "2006-10-09T22:06:23Z"
tags: ["J#", "JDBC", "MySQL", "SQL"]
slug: "untitled"
draft: false
lastmod: "2006-10-09T22:06:23Z"
id: 26680
url: "https://zhieer.iteye.com/blog/26680"
---

Class.forName("org.g<span style="color:red">j</span>t.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();
		}
	}

}
```