JDBC MySQL 数据库相关操作

操作数据库基本代码:
 package cloudnote;

import java.sql.*;


public class DB {
	public static Connection getConn() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost/cloudnote?user=root&password=yingjun&characterEncoding=utf-8");
		} 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 的区别:

1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程

2.使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。PreparedStatement 对象的开销比Statement大,对于一次性操作并不会带来额外的好处。

3.statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译得,   preparedstatement支持批处理,可以防止sql注入。

4.执行许多SQL语句的JDBC程序产生大量的Statement和PreparedStatement对象。通常认为PreparedStatement对象比Statement对象更有效,特别是如果带有不同参数的同一SQL语句被多次执行的时候。PreparedStatement对象允许数据库预编译SQL语句,这样在随后的运行中可以节省时间并增加代码的可读性。

猜你喜欢

转载自wosyingjun.iteye.com/blog/1817753