create table denglu(
uname varchar(30),
upwd varchar(30)
)
insert into denglu values('诗书','123')
insert into denglu values('画唱','456')
--drop table denglu
package JDBC;
import java.sql.*;
import java.util.Scanner;
public class dengLu {
public static void main(String[] args) throws Exception{
Scanner s=new Scanner(System.in);
System.out.println("请输入用户名");
String uname=s.next();
System.out.println("请输入用户密码");
String upwd=s.next();
//1、导入数据包(就是把sqljdbc4-2.0.jar导入文件夹中某处的里面)
//2、加载数据包(静态加载类加载这个包,就是用下面的代码)
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//3、建立数据库链接的对象Connection
//4.链接的数据库位置(jdbc:sqlserver://localhost;)
//5.数据库名6.用户名7.密码
//jdbc:sqlserver://localhost;databaseName=yonghu,qqq,123
Connection con=
DriverManager.getConnection("jdbc:sqlserver://localhost;"
+ "databaseName=yonghu","qqq","123");
//8、建立执行sql语句的对象(statement)
String sql="select * from denglu where uname=? and upwd=?";
System.out.println(sql);
//用Statement sta=con.createStatement();会有注入攻击,所以不用。
PreparedStatement ps=con.prepareStatement(sql);//传入sql语句
ps.setObject(1,uname);//"n"代表SQL语句中的第n个"?"
ps.setObject(2,upwd);
//9、写sql语句,执行
//10、建立得到数据的对象(ResultSet)
ResultSet res=ps.executeQuery();
//11、遍历数据
while(res.next()){
System.out.println("用“getObject(1)”得到的用户名为:"
+res.getObject(1)+";");
System.out.println("用“getObject(列名)”得到的用户名为:"
+res.getObject("uname")+" ;");
System.out.println("用“getObject(列名)”得到的密码为: "
+res.getObject("upwd"));
//12、关闭链接
//什么是防注入?直接字符串拼接,用户再后输入一个or 1=1的时候,那么这个
//条件是肯定成立的,所以用户输入不输入用户名没有多大的关系了
//为了解决这个问题,就要使用preparedstatement解决,它默认将
//sql语句编译成函数,每次只要传入相应的参数就可以了,就可以
//防止用户sql注入。
//预处理:我们每次对于数据库操作数据库都要将sql语句编译成函数,
//如果我们使用预处理的时候,数据库就默认生成了一个函数,以后再
//传入相同的内容的时候,只需要调用这个函数就可以了,这个就可以提升
//sql执行的效率。
}
}
}
建立学生表,包含学生编号,姓名,年龄,性别,地址,增加学生实体类,编写返回学生信息的集合,制作增删改查,要求使用预处理
create table student(
id int primary key identity(1, 1),
name nvarchar (20) ,
age int check (Age>5 and Age<60),
sex nvarchar (10) check(sex='男'or sex='女'),
address nvarchar (20)
)
insert into student values('诗书',19,'男','江西')
insert into student values('江唯',20,'女','湖南')
insert into student values('嘉怡',21,'女','上海')
insert into student values('画唱',22,'男','北京')
insert into student values('点赞',20,'男','武汉')
--select * from student
--delete from student
--drop table student
package StudentJDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import JDBC.renYuanLeiXing;
public class JDBCZSGC {
//SelectDanGeRenYuan搜索单个人员
public static ResultSet SelectDanGeStudent = null;
public static Scanner s = new Scanner(System.in);
public static void bianLiStudentFangFa
(ArrayList studentJiHe) {
for (student i : studentJiHe) {
System.out.println(i);
}
}
//
//chuanZhiDaoJiHeDeFangFa:为传值到集合的方法,
//会把sql执行时,调用这个方法得到的值,传值时,查到内容时
//,查到的内容都会加到ArrayList集合studentJiHe中,
//studentJiHe的才可能有内容,size()大小值才可能不为0
//
//size()大小值>0来判断,集合是否有内容
//
//chuanZhiDaoJiHeDeFangFa(String sql)前面的
//
//public static ArrayList表示
//
//之后返回的类型为只接受student类的内容的ArrayList集合,
//比如下面的return studentJiHe;就可以证实
//自己总结的语法:
//public static 类型 被命名的名;或public static 类型
//
//方法名(类型 被传的值的被命名的名)
public static ArrayList
chuanZhiDaoJiHeDeFangFa(ResultSet res) {
ArrayList studentJiHe =
new ArrayList();
try {
while (res.next()) {
student studentClass =
new student();
studentClass.setId
(res.getInt(1));
studentClass.setName
(res.getString(2));
studentClass.setAge
(res.getInt(3));
studentClass.setSex
(res.getString(4));
studentClass.setAddress
(res.getString(5));
studentJiHe.add(studentClass);
//
//
//以上是把内容加到集合中,不这么写,
//下面的增删改查等的集合长度都会为0,集合中会没内容,
//
//这里getStudent(String sql)
}
} catch (SQLException e) {
e.printStackTrace();
}
return studentJiHe;
}
public static void main(String[] args)
throws Exception {
boolean b = true;
while (true) {
renYuanFengZhuangFangFa();
}
}
private static void renYuanFengZhuangFangFa()
throws ClassNotFoundException,
SQLException {
System.out.println("选项:1.打印所有的人员信息\n"
+ "2.输入编号修改指定人员信息\n"
+ "3.输入编号查询单个人员信息\n"
+ "4.删除指定人员信息\n"
+ "5.增加人员信息");
int xuanXiangAll = s.nextInt();
//
//xuanXiangAll,xuanXiangAll_a等的命名方式可让我知道
//xuanXiangAll_a是被某个xuanXiangAll套住的,
//xuanXiangAll_a在xuanXiangAll里面
if (xuanXiangAll == 1) {
Connection con = bianLiAllStudentXinXiFangFa();
//
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//
//
//
//Connection con = DriverManager.getConnection(
//
//
//
//"jdbc:sqlserver://localhost;databaseName=yonghu", "qqq",
//
//
//
//"123");
//
//
//
//String sql = "select * from student ";
ResultSet res = DBUtils.Select(sql);
这一句不可用,就算DBUtils中用了prepareStatement也不行
//
//
//PreparedStatement ps = con.prepareStatement(sql);
//
//ResultSet res =ps.executeQuery();
//
//
用PreparedStatement时,用executeQuery();其余用executeQuery(sql);
//
//
//while (res.next()) {
//
//
//
//System.out.println("编号:"+res.getInt("id") + ";\t"
//
//
//
//+"名字:"+ res.getString("name") + ";\t"
//
//+"年龄:"+ res.getInt("age") + ";\t"
//+"性别:"+ res.getString("sex") + ";\t"
//+"地址:"+ res.getString("address")+ ";\t");
//
//
//
//}
}
else if (xuanXiangAll == 2) {
System.out.println("---执行修改操作---");
System.out.println("请输入你要修改的人员编号");
bianLiAllStudentXinXiFangFa();
int id = s.nextInt();
String sqlDanGe = "select * from student where id=?";
Connection con = bianLiAllStudentXinXiFangFa();
PreparedStatement ps1=con.prepareStatement(sqlDanGe);
ps1.setObject(1,id);
System.out.println(sqlDanGe);
//ResultSet res1 = ps1.executeQuery();
SelectDanGeStudent = ps1.executeQuery();
ArrayList studentJiHe = chuanZhiDaoJiHeDeFangFa
(SelectDanGeStudent);//return出,打印出内容
System.out.println(studentJiHe.size());
if (studentJiHe.size()>0) {
//danGeRenYuanXinXi单个人员信息
for (student danGeRenYuanXinXi : studentJiHe)
{
System.out.println(danGeRenYuanXinXi);
}
System.out.println("请选择你要修改该人员的哪项内容");
System.out.println("1.人员名字2.人员地址");
int xuanXiangAll_a = s.nextInt();
if (xuanXiangAll_a == 1) {
System.out.println("请输入该人员的新名字");
String newName = s.next();
String sqlXiuGai = "update student set name=?"
+ " where id=?";
//报错为对象错,就可能为SQL语句中的表名错了
PreparedStatement ps11=con.prepareStatement(sqlXiuGai);
//自己总结的语法:prepareStatement(要执行的SQL语句),
//要执行的SQL语句不要写错或共用了,下面的ps11位置的内容要
//和上面的ps11位置的内容一样的内容
ps11.setObject(1,newName);
ps11.setObject(2,id);
if (ps11.executeUpdate() > 0) {
System.out.println("名字修改成功");
bianLiAllStudentXinXiFangFa();
} else {
System.out.println("修改失败,请重试");
}
} else if (xuanXiangAll_a == 2) {
System.out.println("请输入新地址");
String newDiZhi = s.next();
String sqlXiuGai = "update student set address=?"
+ " where id=?";
PreparedStatement ps11=con.prepareStatement(sqlXiuGai);
ps11.setObject(1,newDiZhi);
ps11.setObject(2,id);
if (ps11
.executeUpdate() > 0) {
System.out.println("修改成功");
bianLiAllStudentXinXiFangFa();
} else {
System.out.println("修改失败,请重试");
}
}
} else {
System.out.println("编号输入错误");
}
}
else if (xuanXiangAll == 3) {
System.out.println("---执行查询单个人员信息的操作---");
System.out.println("请输入你要查询的人员ID");
bianLiAllStudentXinXiFangFa();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu", "qqq",
"123");
int id = s.nextInt();
String sql = "select * from student where "
+ "id=?";
PreparedStatement ps=con.prepareStatement(sql);//传入sql语句
ps.setObject(1,id);
ResultSet res = ps.executeQuery();
//res.getObject("uname");
while (res.next()) {
System.out.println("编号:"+res.getInt("id") + ";\t"
+"名字:"+ res.getString("name") + ";\t"
+"年龄:"+ res.getInt("age") + ";\t"
+"性别:"+ res.getString("sex") + ";\t"
+"地址:"+ res.getString("address")+ ";\t");
}
}
else if (xuanXiangAll == 4) {
System.out.println("---执行删除操作---");
System.out.println("请输入你要删除的学生ID");
//bianliAllStudentXinXi()
//为我用鼠标右键封装的遍历所有学生信息的方法,调用同一个
Connection con = bianLiAllStudentXinXiFangFa();
//因为下面有con所以这里要声明Connection con
int deleteId = s.nextInt();
String sql2 = "delete student where id=?";
PreparedStatement ps2=con.prepareStatement(sql2);
ps2.setInt(1, deleteId);
//ResultSet res2 = ps2.executeQuery();这句这里不必些写,因为下面没必要遍历
//
//自己的总结:以下顺序不可错:1.接受输入
//2.SQL语句
//3.用prepareStatement();执行SQL语句
//4.用setInt();
if (ps2.executeUpdate() > 0) {
System.out.println("删除成功");
bianLiAllStudentXinXiFangFa();
//自己总结的调用方法的语法:方法名();
} else {
System.out.println("删除失败,请重试");
}
}
else if (xuanXiangAll == 5) {
bianLiAllStudentXinXiFangFa();
System.out.println("---执行增加操作---");
Class.forName("com.microsoft.sqlserver."
+ "jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu", "qqq",
"123");
//
//System.out.println("请输入要增加的编号");
//
//编号id在SQL中设置为自增,所以不必加入
//
//int insertId = s.nextInt();
System.out.println("请输入要增加学生的名字");
String insertName = s.next();
System.out.println("请输入要增加学生的年龄(60>年龄>5)");
int insertAge = s.nextInt();
System.out.println("请输入要增加学生的性别(只可以填“男”或“女”)");
String insertSex = s.next();
System.out.println("请输入要增加学生的地址");
String insertAddress = s.next();
String sql = "insert into student values(?,?,?,?)";
PreparedStatement ps=con.prepareStatement(sql);
ps.setObject(1,insertName);
ps.setObject(2,insertAge);
ps.setObject(3,insertSex);
ps.setObject(4,insertAddress);
if (ps.executeUpdate() > 0) {
System.out.println("添加成功!");
bianLiAllStudentXinXiFangFa();
//自己总结的调用方法的语法:方法名();
} else {
System.out.println("添加失败,请重试!");
}
}
else {
System.out.println("编号输入错误");
}
}
private static Connection bianLiAllStudentXinXiFangFa()
throws ClassNotFoundException, SQLException {
Class.forName("com.microsoft.sqlserver."
+ "jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu",
"qqq",
"123");
String sql = "select * from student";
PreparedStatement ps=con.prepareStatement(sql);
ResultSet res = ps.executeQuery();
System.out.println("_________");
while (res.next()) {
System.out.println("编号:"+res.getInt("id") + ";\t"
+"名字:"+ res.getString("name") + ";\t"
+"年龄:"+ res.getInt("age") + ";\t"
+"性别:"+ res.getString("sex") + ";\t"
+"地址:"+ res.getString("address")+ ";\t");
}
System.out.println("_________");
return con;
}
}
package StudentJDBC;
import JDBC.renYuanLeiXing;
public class student {
private int id;
private String name;
private int age;
private String sex ;
private String address ;
//
//
//private renYuanLeiXing renYuanLeiXingID;
public student() {
}
public int getRenYuanID() {
return id;
}
public void setId(int renYuanID) {
this.id = renYuanID;
}
public String getRenYuanDiZhi() {
return name;
}
public void setName1(String renYuanDiZhi) {
this.name = renYuanDiZhi;
}
public student(int id, String name,
int age, String sex, String address) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.address = address;
}
@Override
public String toString() {
return "student [id=" + id + ", name="
+ name + ", age=" + age + ", sex="
+ sex + ", address=" + address + "]";
}
public int getId() {
return id;
}
public void setId1(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}