日期:2014-05-16  浏览次数:20359 次

JDBC MySQL 数据库相关操作
写道
操作数据库基本代码
?package cloudnote;

import java.sql.*;


public class DB {
	public static Connection getConn() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");// 通过class.forname加载数据库驱动,此处加载 mysql驱动
			conn = DriverManager.getConnection("jdbc:mysql://localhost/cloudnote?user=root&password=yingjun&characterEncoding=utf-8");// 使用java.sql包下的drivermanager类打开数据库连接
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return conn;
	}
	
	/**
	 * 如果应用程序中的多次执行都要用到同一条sql语句,将使应用程序的效率明显降低,这时可以使用preparedStatement对象来解决。
	 * 此对象第一次执行的时候将sql语句传给数据库预编译,从而在以后执行这个sql语句时速度都能得到很大的提高。
	 * 建立的连接
	 * 要执行的sql语句
	 * @return
	 */
	public static PreparedStatement prepare(Connection conn,  String sql) {
		PreparedStatement pstmt = null; 
		try {
			if(conn != null) {
				pstmt = conn.prepareStatement(sql);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pstmt;
	}
	//该对象能获取自动生成的键。给定常量告知驱动程序是否可以获取自动生成的键
	public static PreparedStatement prepare(Connection conn,  String sql, int autoGenereatedKeys) {
		PreparedStatement pstmt = null; 
		try {
			if(conn != null) {
				pstmt = conn.prepareStatement(sql, autoGenereatedKeys);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pstmt;
	}
	
	public static Statement getStatement(Connection conn) {
		Statement stmt = null; 
		try {
			if(conn != null) {
				stmt = conn.createStatement();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stmt;
	}
	

	
	public static ResultSet getResultSet(Statement stmt, String sql) {
		ResultSet rs = null;
		try {
			if(stmt != null) {
				rs = stmt.executeQuery(sql);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	
	public static void close(ResultSet rs,Statement stmt,Connection conn) {  
        try {   
	        	if(rs != null) {  
	                rs.close();  
	                rs = null;  
	            } 
	        	if(stmt != null) {  
	                stmt.close();  
	                stmt = null;  
	            }  
	            if(conn != null) {  
	                conn.close();  
	                conn = null;  
	            }  
          } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }  
      	
}
?
      //修改数据(PrepareStatement) 
      String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); 
      Connection conn =DB.getConn(); 
      String sql = "insert into note(`title`,`content`,`time`,`username`) values('"+title+"','"+content+"','"+time+"','"+username+"')"; 
      PreparedStatement pstmt=DB.prepare(conn,sql);
      pstmt.executeUpdate(); 
      DB.close(null,pstmt,conn); 
     //修改数据(Statement)
     String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
     Connection conn =DB.getConn(); 
     String sql = "insert into note(`title`,`content`,`time`,`username`) values('"+title+"','"+content+"','"+time+"','"+username+"')"; 
     Statement stmt=DB.getStatement(conn); 
     stmt.executeUpdate(sql);
     DB.close(null,stmt,conn);
    //更新数据(PreparedStatement)
     Connection conn =DB.getConn();
     String sql="select * from note where usernmae='"+username+"'";
     PreparedStatement pstmt=DB.prepare(conn,sql);
     ResultSet rs=pstmt.executeQuery();
     DB.close(rs,pstmt,conn);
     //更新数据(PreparedStatement)
     Connection conn =DB.getConn();
     String sql="select * from note where usernmae='"+username+"'";
     Statement stmt = DB.getStatement(conn);
     ResultSet rs=stmt.executeQuery(sql);
     DB.close(rs,stmt,conn);
preparedStatement和Statement 的区别