JDBC之Insert方法

创建4个类:StuInfo(实体类)、DBConn(连接、关闭数据库)、DBUtil(数据库操作)、RunMain(测试类)

1.StuInfo实体类:

package com.lykion;

public class StuInfo {

	private String sno;
	private String sname;
	private String dname;
	private String ssex;
	private int cno;
	private double mark;
	private String type; 
	
	public StuInfo() {
		
	}
	
	public StuInfo(String sno, String sname, String dname, String ssex, int cno, double mark, String type) {
		super();
		this.sno = sno;
		this.sname = sname;
		this.dname = dname;
		this.ssex = ssex;
		this.cno = cno;
		this.mark = mark;
		this.type = type;
	}
	
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	
	public String getSsex() {
		return ssex;
	}
	public void setSsex(String ssex) {
		this.ssex = ssex;
	}
	
	public int getCno() {
		return cno;
	}
	public void setCno(int cno) {
		this.cno = cno;
	}
	
	public double getMark() {
		return mark;
	}
	public void setMark(double mark) {
		this.mark = mark;
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
}

2.DBConn类,对数据库的操作:打开数据库(连接数据库),操作完成后关闭数据库,释放资源

package com.lykion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConn {

	private static final String url = "jdbc:mysql://localhost:3306/test";		//数据库地址
	private static final String username = "root";		//数据库用户名
	private static final String password = "123456";		//数据库密码
	private static final String driver = "com.mysql.jdbc.Driver";		//mysql驱动
	private static final Connection conn = null;
	
	/**
	 * 连接数据库
	 * @return
	 */
	public static Connection conn() {
		Connection conn = null;
		try {
			Class.forName(driver);  //加载数据库驱动
			try {
				conn = DriverManager.getConnection(url, username, password);  //连接数据库
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 关闭数据库链接
	 * @return
	 */
	public static void close() {
		if(conn != null) {
			try {
				conn.close();  //关闭数据库链接
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

3.DBUtil类:主要是数据常用的操作,实例中主要是实现Insert操作

package com.lykion;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class DBUtil {

	private static Connection conn = null;
	private static PreparedStatement ps = null;
	private static ResultSet rs = null;
	private static final CallableStatement cs = null;
	
	/**
	 * Insert方法封装
	 * @param stu 传入参数
	 */
	public static void Insert(StuInfo stu) {
		conn = DBConn.conn();		//调用 DBconnection 类的 conn() 方法连接数据库
		String sql = "INSERT INTO student01 (sno,sname,dname,ssex,cno,mark,type) VALUES(?,?,?,?,?,?,?)";		//插入sql语句
		try {
			ps = conn.prepareStatement(sql);
			
			/**
			 * 调用实体StuInfo类,获取需要插入的各个字段的值
			 * 注意参数占位的位置
			 * 通过set方法设置参数的位置
			 * 通过get方法取参数的值
			 */
			ps.setString(1, stu.getSno());
			ps.setString(2, stu.getSname());
			ps.setString(3, stu.getDname());
			ps.setString(4, stu.getSsex());
			ps.setInt(5, stu.getCno());
			ps.setDouble(6, stu.getMark());
			ps.setString(7, stu.getType());
			
			ps.executeUpdate();			//执行sql语句
			
			System.out.println("插入成功(* ̄︶ ̄)");
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBConn.close();
		}
	}
	
}

4.RunMain类:测试类,测试DBUtil中的方式是否实现

package com.lykion;

public class RunMain {

	public static void main(String[] args) {
		StuInfo stu = new StuInfo("9006", "波波菜", "计算机科学与软件工程", "女", 8, 99.5, "必修");
		DBUtil.Insert(stu);
	}
}


你可能感兴趣的:(数据库,mysql)