不使用框架,使用JDBC操作数据库

//首先需要到maven仓库下载jar包,直接搜索mysql就可以,下载mysql/j的jar包;
//导入到eclipse或idea中

/**
 * 学生实体类
 * 我数据库中有一个学生表,学生表的各个属性抽象成student类的属性
 * @author changqing
 *
 */

import java.util.Date;

public class Student {
	private Long id;
	private String name;
	private Character sex;
	private Date birthday;
	public Student() {
		super();
	}
	public Student(Long id, String name, Character sex, Date birthday) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.birthday = birthday;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Character getSex() {
		return sex;
	}
	public void setSex(Character sex) {
		this.sex = sex;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + "]";
	}
}

			
			

package org.jgs2010.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jgs2010.entity.Student;

/**
 * dao :数据访问对象
 * @author Administrator
 *
 */
public class StudentDao {
	/**
	 * @param student 学生实体类
	 * @return 影响的行数
	 */
	
	//插入
	public int insert(Student student) {
		String sql = "insert into student(`name`,sex,birthday) value ( '"+student.getName()+"','"+student.getSex()+"','"+new SimpleDateFormat("yyyy-MM-dd").format(student.getBirthday())+"') ";
		
		try {
			//注册驱动
				//com.mysql.cj.jdbc.Driver这个是jar包内驱动的路径,照写就行
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Connection connection =null;
		try {
			//获取连接
			//jdbc:mysql://localhost/jdbctest这段前半部分照写,后半部分是要操作的数据库的名字
			//root是数据库用户名,一般都是root
			//123456是数据库的登入密码
			connection = DriverManager.getConnection("jdbc:mysql://localhost/jdbctest", "root", "123456");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Statement statement = null;
		try {
			//获取语句执行者
			statement = connection.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int result = 0;
		try {
			//执行sql语句
			result = statement.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		 //释放资源
		try {
			statement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	
	
	
	/**
	 * 	修改一个学生
	 * @param student 学生实体
	 * @return 影响的行数
	 */
	//修改
	public int update(Student student ) {
		
		try {
			//注册驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Connection connection2 = null;
		Statement statement2 =  null;
		int update = 0;
		String sql2 = "update student set birthday = '"+new SimpleDateFormat("yyyy-MM-dd").format(student.getBirthday())+"' where id = '"+student.getId()+"'";
		
		try {
			//获取连接
			connection2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest", "root", "123456");
			//获取语句执行者
			statement2 = connection2.createStatement();
			update = statement2.executeUpdate(sql2);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				statement2.close();
				connection2.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		return update;
	
	}
	
	
	
	
	
	/**
	 * 	删除一个学生
	 * @param id 学生ID
	 * @return 影响的行数
	 */
	//删除
	public int delete(Long id) {
		//注册驱动
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Connection connection3 = null;
		Statement statement = null;
		int update2 = 0;
		String sql3 = "delete from student where id = "+id;
		try {
			//创建连接
			connection3 = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest", "root", "123456");
			//获取语句执行者
		   statement = connection3.createStatement();
		   //执行语句
		   update2 = statement.executeUpdate(sql3);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				connection3.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		return update2;
	}

}










import java.util.Date;

import org.jgs2010.dao.StudentDao;
import org.jgs2010.entity.Student;

public class Demo02 {
	public static void main(String[] args) {
		StudentDao studentDao = new StudentDao();
		//int insert1 = studentDao.insert(new Student(null,"王五",'男',new Date()));
		//System.out.println(insert1);
		//int update = studentDao.update(new Student((long) 2,null,null,new Date()));
		//System.out.println(update);
		int delete = studentDao.delete((long)3);
		System.out.println(delete);
		
		
		
	}

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值