【Java Web开发】关于数据库的操作

1.mysql连接数据库并创建数据库连接对象

第一种:

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaweb?user=root&password=welcom123");

第二种:

public class DBConnection {
	
	private static String url="jdbc:mysql://localhost:3306/javaweb";
	private static String user = "root";
	private static String password="welcome123";
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/*
	 * 获取连接对象
	 */
	public static Connection getConn(){
		Connection conn = null;
		try{
			conn = (Connection) DriverManager.getConnection(url,user,password);
		}catch(SQLException e){
			e.printStackTrace();
		}
		return conn;
	}

2.对数据库进行增删查改

public static void insert(String sno,String name,String sex,int age){
		Connection conn = null;
		PreparedStatement ps=null;
		try {
			conn = DBConnection.getConn();
			String sql = "insert into students(no,name,sex,age) values(?,?,?,?)";
			ps =(PreparedStatement) conn.prepareStatement(sql);
			ps.setString(1,sno);
			ps.setString(2,name);			
			ps.setString(3, sex);
			ps.setInt(4, age);
			ps.executeUpdate();			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();			
		}finally{
			try {
				if(null!=ps){
					ps.close();
				}
				if(null!=conn){
					conn.close();
				}
				
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
public static void delById(int id){
		Connection conn = null;
		String sql = "delete from students where id = ?";
		PreparedStatement ps = null;
		try {
			conn = DBConnection.getConn();
			ps =(PreparedStatement) conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{			
				try {
					if(null !=ps){
						ps.close();
					}
					if(null !=conn){
						conn.close();
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
		}
		
		
	}

第一种:根据id查

public static Student queryById(int id){
		String sql = "select * from students where id = ?";
		Connection conn = null;
		PreparedStatement ps = null;	
		ResultSet res = null;
		Student student = null;
		try {
			conn = DBConnection.getConn();
			ps =(PreparedStatement) conn.prepareStatement(sql);
			ps.setInt(1, id);
			res = ps.executeQuery();			
			while(res.next()){
				student = new Student(res.getInt(1),res.getString(2), res.getString(3), res.getString(4), res.getInt(5));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try{
				if(null!=res){
					res.close();				
				}
				if(null!=ps){
					ps.close();
				}
				if(null!=conn){
					conn.close();
				}
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		return student;
	}

第二种:查询所有

public static List queryAll(){
		List list=new ArrayList();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		try {
			 conn = DBConnection.getConn();
			 ps = (PreparedStatement)conn.prepareStatement("SELECT * from students");
			 res = ps.executeQuery();
			 while(res.next()){
				 list.add(new Student(res.getInt(1),res.getString(2),res.getString(3),res.getString(4),res.getInt(5)));
			 }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(null!=res){
					res.close();
				}
				if(null!=ps){
					ps.close();
				}
				if(null!=conn){
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
public static void updateById(int id,String no,String name,String sex,int age){
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "UPDATE students SET no=?,name =?,sex=?,age=? WHERE id = ? ";
		try {
			conn = DBConnection.getConn();
			ps = (PreparedStatement)conn.prepareStatement(sql);
			ps.setString(1, no);
			ps.setString(2, name);
			ps.setString(3, sex);
			ps.setInt(4, age);
			ps.setInt(5, id);
			ps.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {				
				if(null!=ps){
					ps.close();
				}
				if(null!=conn){
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

数据库操作的其他方法

方法 参数类型 返回值类型 适用位置
Statement.executeUpdate(sql) String,不带未知值 不带返回的insert、update、delete操作
Statement.executeQuery(sql) String,不带未知值 ResultSet 知道参数的查询
Statement.getFetchSize() int,获取ResultSet的设置大小 预先setFetchSize(n),可通过该方法获取
Statement.setMaxRows(max) int 设置ResultSet最大包含行数
PreparedStatement.clearParameters() 需要重复使用预处理时,清除当前参数值
Statement.execute(sql) String 判断结果是否为ResultSet对象 执行任何指定的sql语句

PreparedStatement.executeQuery()

ResultSet 已经设置好预处理sql语句
PreparedStatement.executeUpdate() 设置好预处理sql,处理无返回内容的sql语句
PreparedStatement.getMetaData() ResultSetMetaData 对象 获取ResultSet对象的列描述



你可能感兴趣的:(JavaWeb)