目录
1,Mysql数据库连接池对象
package dao;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
private static DataSource ds;
static {
Properties pro = new Properties();
try {
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("dao/druid.properties"));
ds= DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static DataSource getDataSource(){
return ds;
}
public static void close(Statement stmt, Connection conn){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet re,Statement stmt,Connection conn){
try {
re.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.加入druid.properties
driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/ljh02 username=root password=021012 initialSize=5 maxActive=10 maxWait=3000
3.查找的工具类
package CRUE;
import dao.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class ChaById {
public static JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
static Scanner sc = new Scanner(System.in);
public static void selectById(){
System.out.println("请输入要查找信息的学号");
int id = sc.nextInt();
String sql = "select *from student where id = "+id;
List<Map<String, Object>> maps = jt.queryForList(sql);
for (Map<String, Object> map : maps) {
System.out.println(map);
}
}
public static void selectByName(){
System.out.println("请输入要查找信息的名字");
String name = sc.next();
String sql = "select * from student where name = '"+name+"'";
List<Map<String, Object>> maps = jt.queryForList(sql);
for (Map<String, Object> map : maps) {
System.out.println(map);
}
}
public static void selectAll(){
System.out.println("查找全部信息");
String sql = "select * from student";
List<Map<String, Object>> maps = jt.queryForList(sql);
for (Map<String, Object> map : maps) {
System.out.println(map);
}
}
}
4,通过ID属性删除学生
package CRUE;
import dao.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.Scanner;
public class ShanById {
public static void delete(){
Scanner sc = new Scanner(System.in);
JdbcTemplate Jt = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "delete from student where id = ?";
System.out.printf("请输入需要删除同学的学号:");
int id = sc.nextInt();
int update = Jt.update(sql, id);
if (update!=0) {
System.out.println("删除成功");
}
}
}
5,更新
package CRUE;
import dao.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Update {
public static JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
public static void UpdateByName(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学号来确认人的名字");
String str = sc.next();
System.out.println("请输入新的名字");
String name = sc.next();
String sql = "Update student set name = '"+name+"' where id = '"+str+"'";
int update = jt.update(sql);
if(update!=0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
}
public static void UpdateByAge(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学号来确认人的名字");
String str = sc.next();
System.out.println("请输入新的年龄");
int age = sc.nextInt();
String sql = "Update student set age = '"+age+"' where id = '"+str+"'";
int update = jt.update(sql);
if(update!=0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
}
}
6.增加学生
package CRUE;
import dao.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.Scanner;
public class Zeng {
public static void addd(){
JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
Scanner sc = new Scanner(System.in);
String sql = "insert into student values (?,?,?,?,?,?,?)";
System.out.println("请输入姓名");
String name = sc.next();
System.out.println("请输入学号");
int id = sc.nextInt();
System.out.println("请输入性别");
String grender = sc.next();
System.out.println("请输入年龄");
int age = sc.nextInt();
System.out.println("请输入体重");
double weight = sc.nextDouble();
System.out.println("请输入身高");
double hight = sc.nextDouble();
System.out.println("请输入爱好(每个爱好拿英文逗号隔开)");
String hobby = sc.next();
int update = jt.update(sql, id,name,grender,age,weight,hight, hobby);
if(update!=0){
System.out.println("添加成功");
}
}
}
7,主函数
import CRUE.ChaById;
import CRUE.ShanById;
import CRUE.Update;
import CRUE.Zeng;
import java.util.Scanner;
/**
* 利用数据库实现
* 数据由数据库中导入,然后进行sql更改,返回值数据库
*/
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
pro();
System.out.println("请输入功能序号");
int num = sc.nextInt();
while(num!=0){
switch (num){
case 1 :
Zeng.addd();
break;
case 2:
ShanById.delete();
break;
case 3:
swi();
break;
case 4:
swi2();
break;
}
pro();
System.out.println("请输入功能序号");
num = sc.nextInt();
}
}
public static void swi2(){
Scanner sc = new Scanner(System.in);
pro3();
System.out.println("请输入查找的功能数字");
int num = sc.nextInt();
switch (num){
case 1:
Update.UpdateByName();
break;
case 2:
Update.UpdateByAge();
break;
}
}
public static void pro3(){
System.out.println("1,修改名字");
System.out.println("2,修改年龄");
}
public static void swi(){
Scanner sc = new Scanner(System.in);
pro2();
System.out.println("请输入查找的功能数字");
int num = sc.nextInt();
switch (num){
case 1:
ChaById.selectById();
break;
case 2:
ChaById.selectByName();
break;
case 3:
ChaById.selectAll();
break;
}
}
public static void pro(){
System.out.println("功能");
System.out.println("0,退出");
System.out.println("1,增加信息");
System.out.println("2,按照学号删除");
System.out.println("3,查找");
System.out.println("4,更改");
}
public static void pro2(){
System.out.println("功能");
System.out.println("1,利用学号查找");
System.out.println("2,利用名字查找");
System.out.println("3,查找全部信息");
}
}