首先让大家看一下我定义的Student这个类里面有啥方便大家理解后续代码
package JDBC.Bigdata.Student;
import java.io.File;
public class Student {
public int getSno() {
return Sno;
}
public void setSno(int sno) {
Sno = sno;
}
public String getSname() {
return Sname;
}
public void setSname(String sname) {
Sname = sname;
}
public String getSsex() {
return Ssex;
}
public void setSsex(String ssex) {
Ssex = ssex;
}
public String getSc() {
return sc;
}
public void setSc(String sc) {
this.sc = sc;
}
public int getSage() {
return Sage;
}
public void setSage(int sage) {
Sage = sage;
}
@Override
public String toString() {
return "Student{" +
"Sno=" + Sno +
", Sname='" + Sname + '\'' +
", Ssex='" + Ssex + '\'' +
", sc='" + sc + '\'' +
", Sage=" + Sage +
'}';
}
public Student(int sno) {
Sno = sno;
}
private int Sno;
private String Sname;
public Student(int sno, int sage) {
Sno = sno;
Sage = sage;
}
private String Ssex;
private String sc;
private int Sage;
public File getContext() {
return context;
}
public void setContext(File context) {
this.context = context;
}
public Student(int sno, String sname, String ssex, String sc, int sage, File context) {
Sno = sno;
Sname = sname;
Ssex = ssex;
this.sc = sc;
Sage = sage;
this.context = context;
}
private File context;
private File pic;
public Student(int sno, String sname, String ssex, String sc, int sage, File context, File pic) {
Sno = sno;
Sname = sname;
Ssex = ssex;
this.sc = sc;
Sage = sage;
this.context = context;
this.pic = pic;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
public Student(int sno, String sname, String ssex, String sc, int sage) {
Sno = sno;
Sname = sname;
Ssex = ssex;
this.sc = sc;
Sage = sage;
}
}
这里面Studnet里面都是些很基础的代码,像什么构造了,tostring了,一键可以直接出来,这就不过多叙述了。
package JDBC.Bigdata.test;
import JDBC.Bigdata.util.dbutil;
import java.sql.Connection;
import java.sql.Statement;
public class insert {
public static void main(String[] args) throws Exception{
dbutil dbUtil=new dbutil();
String sql="insert into student values(50,'小垃圾','男','cs',18)";
Connection con=dbUtil.getCon();
Statement stmt=con.createStatement();
int result= stmt.executeUpdate(sql);//这个操作完是一个int类型的数据,可以用一个int来接受这个数据,看操作了几条
System.out.println("操作了"+result+"条数据");
stmt.close();
con.close();
}
}
上面的这个代码就是所谓的增了。还是以前最基础的加上这个Statement来处理sql语句就行了,不懂最基础的可以参考我的那篇文章,https://blog.csdn.net/renhuoyishi99/article/details/95939160
这个文章会对你有帮助的。
接下来是删除操作了。
package JDBC.Bigdata.test;
import JDBC.Bigdata.Student.Student;
import JDBC.Bigdata.util.dbutil;
import java.sql.Connection;
import java.sql.Statement;
public class delete {
public static int del(Student student)throws Exception
{
String sql="delete from student where Sno="+student.getSno();
dbutil dbUtil=new dbutil();
Connection con=dbUtil.getCon();
Statement stmt=con.createStatement();
int result=stmt.executeUpdate(sql);
return result;
}
public static void main(String[] args)throws Exception {
Student student=new Student(50);
int result=del(student);
if(result==1)
{
System.out.println("操作成功");
}
else
{
System.out.println("操作失败");
}
}
}
这其实和增差不多一样。不同的就是我造了一个Student这个类型的一个对象并传了值。到上面可以直接搞出来,就是所谓的get函数吗。
增删查改其实就是sql语句不同,我就不过多的阐述了,代码给大家,大家随意看看理解下吧,其实Statement这个还是有弊端的,一般不建议大家使用,我们用的一般是preparedStatement,这个更好更方便,有兴趣可以看我的下篇博客https://blog.csdn.net/renhuoyishi99/article/details/100181419。
接下来是查和改的代码了。
查的我没找到,哈哈哈,真尴尬,你们自己打下吧,就是sql语句不同,自己钻研下,加油!
package JDBC.Bigdata.test;
import JDBC.Bigdata.Student.Student;
import JDBC.Bigdata.util.dbutil;
import java.sql.Connection;
import java.sql.Statement;
public class update {
public static int up(Student student)throws Exception
{
String sql="update student set Sage=0 where Sno="+student.getSno();
dbutil dbUtil=new dbutil();
Connection con=dbUtil.getCon();
Statement stmt=con.createStatement();
int result=stmt.executeUpdate(sql);
dbUtil.dbClose(con,stmt);
return result;
}
public static void main(String[] args)throws Exception {
Student student=new Student(20);
int result=up(student);
if(result==1)
{
System.out.println("操作成功");
}
else
{
System.out.println("操作失败");
}
}
}