package myproject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MysqlOperation {
public static String user="root";
public static String password="200117x";
//数据库连接方法,自动创建数据库和表
public static Connection getConnection() {
Connection con=null;
Statement stat=null;
try {
Class.forName("com.mysql.jdbc.Driver");
// "jdbc:mysql://localhost:3306/mydatabase?characterEncoding=utf8&useSSL=false"
String url_1="jdbc:mysql://localhost:3306?characterEncoding=utf-8&useSSL=false";
String url_2="jdbc:mysql://localhost:3306/mydatabase?characterEncoding=utf-8&useSSL=false";
// 建立连接
con=DriverManager.getConnection(url_1,user,password);
// 创建数据库mydatabase
stat=(Statement)con.createStatement();
String sql_1="create database if not exists mydatabase;";
// 执行语句创建数据库mydatabase
stat.executeUpdate(sql_1);
// 建立新的连接用于在mydatabase下创建student和alluser表
con=DriverManager.getConnection(url_2,user,password);
String sql_2="create table if not exists student( sno varchar(20) primary key, "
+ "sname varchar(20), sex varchar(20), sage varchar(20), "
+ "phone varchar(20),dorm varchar(20));";
String sql_3="create table if not exists alluser( username varchar(20) primary key, "
+ "password varchar(20))";
stat=(Statement)con.createStatement();
// 执行语句创建student和alluser表
stat.executeUpdate(sql_2);
stat.executeUpdate(sql_3);
}catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 返回连接
return con;
}
// 注册
public static void register(String user_1,String password_1) {
Connection con =null;
Statement stat=null;
try {
con=getConnection();
stat=(Statement)con.createStatement();
String sql="insert into alluser values ('"+user_1+"','"+password_1+"');";
// 执行语句
stat.executeUpdate(sql);
}catch (Exception e) {
e.printStackTrace();
}
}
// 登录检查
public static boolean checkregister(String username,String password) {
Connection con =null;
Statement stat=null;
ResultSet rs=null;
boolean mark=false;
try {
con=getConnection();
stat=(Statement)con.createStatement();
String sql="select * from alluser where username='"+username+"' and password='"+password+"';";
// 执行语句
rs=stat.executeQuery(sql);
mark=rs.next();
}catch (Exception e) {
e.printStackTrace();
}
return mark;
}
//添加学生的方法
public static void addStudent(String sno,String sname,String sex,String sage,String phone,String dorm) {
Connection con =null;
Statement stat=null;
try {
con=getConnection();
stat=(Statement)con.createStatement();
String sql="insert into student values ('"+sno+"','"+sname+"','"
+sex+"','"+sage+"','"+phone+"','"+dorm+"')";
// 执行语句
stat.executeUpdate(sql);
}catch (Exception e) {
e.printStackTrace();
}
}
//删除学生信息
public static void deleteStudent(String sno) {
Connection con =null;
Statement stat=null;
try {
con=getConnection();
stat=(Statement)con.createStatement();
String sql="delete from student where sno='"+sno+"'";
stat.executeUpdate(sql);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//查询学生信息
public static ResultSet selectStudent(String sno) {
Connection con =null;
Statement stat=null;
ResultSet rs=null;
try {
con=getConnection();
stat=(Statement)con.createStatement();
String sql="select * from student where sno='"+sno+"'";
rs=stat.executeQuery(sql);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return rs;
}
//输出全部学生信息
public static ResultSet showStudent() {
Connection con =null;
Statement stat=null;
ResultSet rs=null;
try {
con=getConnection();
stat=(Statement)con.createStatement();
String sql="select * from student;";
rs=stat.executeQuery(sql);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return rs;
}
//修改学生的信息
public static void updateStudent(String sno,String sname,String sex,String sage,String phone,String dorm) {
Connection con =null;
Statement stat=null;
try {
con=getConnection();
stat=(Statement)con.createStatement();
String sql;
//判断是否为空,空值不用修改
if(sname.equals("")) { }
else {
sql="update student set sname='"+sname+"'"+"where sno='"+sno+"'";
//执行语句
stat.executeUpdate(sql);
}
if(sex.equals("")) { }
else {
sql="update student set sex='"+sex+"'"+"where sno='"+sno+"'";
//执行语句
stat.executeUpdate(sql);
}
if(sage.equals("")) { }
else {
sql="update student set sage='"+sage+"'"+"where sno='"+sno+"'";
//执行语句
stat.executeUpdate(sql);
}
if(phone.equals("")) { }
else {
sql="update student set phone='"+phone+"'"+"where sno='"+sno+"'";
//执行语句
stat.executeUpdate(sql);
}
if(dorm.equals("")) { }
else {
sql="update student set dorm='"+dorm+"'"+"where sno='"+sno+"'";
//执行语句
stat.executeUpdate(sql);
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//查询某个学生是否存在,用于检测学号是否重复
public static boolean QueryStudent(String sno) {
Connection con =null;
Statement stat=null;
ResultSet rs=null;
boolean mark=false;
try {
con=getConnection();
stat=(Statement)con.createStatement();
String sql="select * from student where sno='"+sno+"';";
// 执行语句
rs=stat.executeQuery(sql);
mark=rs.next();
}catch (Exception e) {
e.printStackTrace();
}
return mark;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
jsp编写的java Web项目(学生信息管理系统),mysql数据库存储信息,实现了增删改查的基础功能,详情请看我的博客:https://blog.csdn.net/qq_52889967/article/details/117230003
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 30 条
- 1
![avatar](https://profile-avatar.csdnimg.cn/8f46195f6c7049469e4764b08c8cfbaa_qq_52889967.jpg!1)
~浮生~
- 粉丝: 325
- 资源: 20
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
- 1
- 2
- 3
- 4
- 5
- 6
前往页