JDBC連接

轉自:http://java-lyvee.iteye.com/blog/651168 作者:java_lyvee


      package com.test.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * @(#)DataBase.java 010/04/22
 * @author Dudu
 * EST.All rights reserved
 */
public class DataBase {
	/**定義一個Connection 用來連接數據庫*/
	private Connection conn=null;
	/**連接數據庫的URL*/
	private final String url="jdbc:mysql://localhost/lyvee";
	/**指定數據的用戶名和密碼*/
	private final String username="root";
	private final String password="123";
	/**定義一個int記錄更新的記錄數量*/
	int count=0;
	/**定義一個結果集 用於放回查詢結果*/
	private ResultSet resultSet=null;
	private PreparedStatement pstmt=null;
	public DataBase(){
		conn = connectionDB();
	}
	/**
	 * 建立數據的連接
	 * @exception SQLException, ClassNotFoundException
	 */
	@SuppressWarnings("finally")
	public Connection connectionDB(){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection(url,username,password);
			System.out.println("連接數據庫成功");
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("建立數據庫發生錯誤!");
		}finally{
			return conn;
		}
	}
	/**
	 * 查詢方法
	 * @param sql查詢sql語句
	 * @return resultSet
	 */
	@SuppressWarnings("finally")
	public ResultSet query(String sql){
		try {
			pstmt = conn.prepareStatement(sql);
			/**查詢*/
			resultSet = pstmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return resultSet;
		}
	}
	/**
	 * 更新數據
	 * @param sql 更新sql語句
	 * @return
	 */
	public int update(String sql){
		try {
			pstmt = conn.prepareStatement(sql);
			count=pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("執行更新出錯了");
		}
		
		return count;
		
	}
	/**關閉連接*/
	public boolean coles(){
		boolean isColes = false;
		if(resultSet!=null){
			try {
				resultSet.close();
				resultSet=null;
				isColes=true;
			} catch (SQLException e) {
				isColes=false;
				e.printStackTrace();
				System.out.println("關閉結果集發生錯誤");
			}
		}
		if(pstmt!=null){
			try {
				pstmt.close();
				pstmt=null;
				isColes=true;
			} catch (SQLException e) {
				isColes=false;
				e.printStackTrace();
				System.out.println("關閉pstmt發生異常");
			}
		}
		if(conn!=null){
			try{
				conn.close();
				conn=null;
				isColes=true;
			}catch (Exception e) {
				isColes=false;
				e.printStackTrace();
				System.out.println("關閉conn發生異常");
			}
		}
		return isColes;
	}
	/**
	 * 測試查詢的方法
	 * @throws SQLException
	 */
	public void testQuery() throws SQLException{
		resultSet =query("select * from lyvee");
		if(resultSet.next()){
			System.out.println(resultSet.getString(1));
			System.out.println(resultSet.getString(3));
			System.out.println(resultSet.getString(2));
		}
	}
	public void testUpdate(){
		count = update("insert into lyvee(lname,lpwd,lsex)values('tanliang','aaa111','sex')");
		if(count>0){
			System.out.println("更新成功");
		}
	}
	/**
	 * 
	 * @param args
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) throws SQLException, ClassNotFoundException {
		DataBase db = new DataBase();
		/**調用查詢方法*/
		//db.testQuery();
		/**調用更新方法*/
		db.testUpdate();
		/**調用關閉連接方法*/
		db.coles();
		
	}
}


     



發佈了22 篇原創文章 · 獲贊 9 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章