JDBC连接数据库----增删改查

//查询操作

@Test
	public void testQuery()  { 
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet resultSet = null;
		try { 
			//连接数据库
			conn = JDBCUitl.getConnection(); 
			
			//预编译sql
			String sql="select id,name,age,score,birthday from person where id=? ";
			ps = conn.prepareStatement(sql); 
			
			ps.setObject(1, 6); 
			
			//执行操作,返回结果集
			resultSet = ps.executeQuery(); 
			
			//遍历结果集
			if(resultSet.next()) {
				int id = resultSet.getInt(1);
				String name = resultSet.getString(2);
				int age = resultSet.getInt(3);
				double score = resultSet.getDouble(4);
				Date birthday = resultSet.getDate(5);
				
				
			   //返回的结果集封装在Person类中
				Person person=new Person(id, name, age, score, birthday);
				System.out.println(person);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUitl.closeResource(conn, ps, resultSet);
		}	
	} 

**//修改**



@Test
	public void testUPdate()  {
		
		Connection conn=null;
		PreparedStatement ps=null;
		try {  
			//1.获取数据库链接
			conn = JDBCUitl.getConnection();
			
			//2.预编译sql,获取PrepratedStatement实例 
			    String sql="Update student set stu_name=? where stu_id=?";
			     PreparedStatement ps1 = conn.prepareStatement(sql);
			
			//3.填充占位符  
			    ps1.setString(1, "柯震东");
			    ps1.setString(2, "1004");
			    
			//4.执行 
			    ps1.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			
			//5.资源的关闭 
		    JDBCUitl.closeResource(conn, ps);
		}	
	} 

//插入,删除
//向person表中添加一条数据,删除一行数据

@Test
	public void testInsert() {
		     
		    //1.读取配置文件中的四个基本信息
			
			try {
				
				InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
				
				
				 Properties pro = new Properties();
				 pro.load(is); 
				 
				 String user = pro.getProperty("user");
				 String password = pro.getProperty("password");
				 String url = pro.getProperty("url");
				 String driverClass = pro.getProperty("driverClass");
				 
				 //2.加载驱动
				 Class.forName(driverClass);
				 
				//3.获取连接
					Connection conn=null;
					PreparedStatement ps=null;
				 
				 conn = DriverManager.getConnection(url, user, password);
				 
				// 4.预编译sql语句
				 
				 //插入数据
				// String sql="insert into person(id,name,age,score,birthday) values(?,?,?,?,?)";//?占位符
				 String sql="insert into student values(?,?,?,?,?)";//?占位符
				 
				//删除数据
				// String sql="delete from student where stu_id=?";
				 
				 PreparedStatement ps1 = conn.prepareStatement(sql); 
				 
				 //5.填充占位符 
				 
				// ps1.setString(1, "1003");
			
				 
				 ps1.setString(1, "1003");
				 ps1.setString(2,"范冰冰");
				 ps1.setString(3,"女");
				 ps1.setInt(4, 100);
				 ps1.setString(5,"2班"); 
		 
			
				 
				 //6.执行操作
				 ps1.execute();
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				 //7.资源的关闭
				
						try { 
							
							InputStream ps = null;
							if(ps!=null)
						ps.close();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						
						try { 
							InputStream conn = null;
							if(conn!=null)
							conn.close();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}					
				}	

/**
 * 操作数据库的工具类
 * @author ZhaoJiu
 *
 */ 

```java
public class JDBCUitl { 
	/**
	 * 获取数据库的链接
	 * @return
	 * @throws Exception
	 */
	
  public static Connection getConnection() throws Exception {
	  InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
		
		
		 Properties pro = new Properties(); 
		 pro.load(is); 
		 
		 String user = pro.getProperty("user");
		 String password = pro.getProperty("password");
		 String url = pro.getProperty("url");
		 String driverClass = pro.getProperty("driverClass");
		 
		 //2.加载驱动
		 Class.forName(driverClass);
		 
		//3.获取连接
			Connection conn=null;
			PreparedStatement ps=null; 
			
			return conn = DriverManager.getConnection(url, user, password);

  }  
  
  
  /**
   * 关闭资源
   * @param conn
   * @param ps
   */
         public static void closeResource(Connection conn,PreparedStatement ps) {
        	  
        	 try { 
					
				
					if(ps!=null)
				      ps.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				try { 
					
					if(conn!=null)
					  conn.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
         } 
         
         public static void closeResource(Connection conn,PreparedStatement ps,ResultSet rs) {
        	  
        	 try { 
					
 				
					if(ps!=null)
				      ps.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				try { 
					
					if(conn!=null)
					  conn.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}   
				
				try { 
					
					if(rs!=null)
					  rs.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
         }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值