mysql数据库小系统_Mysql数据库基础小实例 学员管理系统菜单
package test;
import java.sql.*;
import java.util.Scanner;
public class testSql002_StudentTest {
/**
* 獲得數(shù)據(jù)庫(kù)連接的
* @return 數(shù)據(jù)庫(kù)連接對(duì)象
* @throws Exception
*/
static Scanner input = new Scanner(System.in);
public static Connection getCon() throws Exception{
String qudong = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/studentdb";
String name = "root";
String pwd = "123";
//加載驅(qū)動(dòng) java和數(shù)據(jù)庫(kù)的橋梁搭建好了
Class.forName(qudong);
//獲得數(shù)據(jù)庫(kù)連接 可以通過(guò)api連接數(shù)據(jù)庫(kù)對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作了
Connection con = DriverManager.getConnection(url,name,pwd);
return con;
}
/*顯示學(xué)員的詳細(xì)信息*/
public static void showStu() throws Exception{
Connection con = getCon();
//顯示stu表中的學(xué)號(hào),姓名,地址????
Statement st = con.createStatement();//執(zhí)行sql語(yǔ)句
String sql = "select * from stu";
//st.executeQuery(sql) //執(zhí)行select 語(yǔ)句的 ResultSet
//st.executeUpdate(sql) //執(zhí)行 insert update delete語(yǔ)句 int 受影響的行數(shù)
ResultSet rs = st.executeQuery(sql);//執(zhí)行查詢(xún)語(yǔ)句獲得結(jié)果
while(rs.next()){//讀取下一行記錄 讀到數(shù)據(jù)返回 true沒(méi)有數(shù)據(jù)false
String stuid = rs.getString(1); //索引值 列名 讀取列的信息
String stuname = rs.getString(2);
String stuaddress = rs.getString("stuaddress");
System.out.println(stuid+"\t"+stuname+"\t"+stuaddress);//控制臺(tái)顯示
}
con.close();//關(guān)閉數(shù)據(jù)庫(kù)連接
//問(wèn)題:讀取所有行 的數(shù)據(jù) while(rs.next())
}
/*添加數(shù)據(jù)*/
public static void addStu() throws Exception{
Connection con = getCon();//獲得數(shù)據(jù)庫(kù)連接對(duì)象
//向表中添加一行數(shù)據(jù)
Statement st = con.createStatement();//工具 執(zhí)行sql
//用戶(hù)輸入數(shù)據(jù),把控制臺(tái)輸入的數(shù)據(jù)添加到數(shù)據(jù)庫(kù)中
System.out.print("請(qǐng)輸入學(xué)號(hào):");
int sno = input.nextInt();
System.out.print("請(qǐng)輸入java成績(jī):");
int sjava = input.nextInt();
System.out.print("請(qǐng)輸入html成績(jī):");
int shtml = input.nextInt();
System.out.print("請(qǐng)輸入sql成績(jī):");
int smysql = input.nextInt();
//String sql = "insert score values("+sno+","+sjava+","+smysql+")";
// %s %f %c %d 格式化字符串,變量列表
String sql = String.format("insert score values(%d,%d,%d,%d)",sno,sjava,shtml,smysql);
int n = st.executeUpdate(sql);// 1
if(n>0){
System.out.println("添加成功");
}else{
System.out.println("添加失敗");
}
con.close();
}
/*修改數(shù)據(jù)*/
private static void updateStu() throws Exception {
Connection con = getCon();
Statement st = con.createStatement();
System.out.println("請(qǐng)輸入學(xué)員的住址:");
String stuaddress = input.next();
String sql =String.format("UPDATE stu SET stuaddress = '%s' WHERE stuaddress ='%s'", stuaddress,"北京");
System.out.println(sql);
int n = st.executeUpdate(sql);
if(n>0){
System.out.println("修改成功");
}else{
System.out.println("修改失敗");
}
con.close();
}
/*刪除學(xué)員信息*/
private static void deleteStu() throws Exception {
Connection con = getCon();
Statement st = con.createStatement();
System.out.println("請(qǐng)輸入要?jiǎng)h除學(xué)員的編號(hào):");
int sid = input.nextInt();
String sql =String.format("DELETE FROM stu WHERE sid =%d", sid);
System.out.println(sql);
int n = st.executeUpdate(sql);
if(n>0){
System.out.println("刪除成功");
}else{
System.out.println("刪除失敗");
}
con.close();
}
public static void main(String[] args) throws Exception{
System.out.println("********學(xué)員管理系統(tǒng)菜單**********");
System.out.println("* 1.查詢(xún)學(xué)員數(shù)據(jù); ");
System.out.println("* 2.添加學(xué)員成績(jī)信息;");
System.out.println("* 3.修改學(xué)員信息;");
System.out.println("* 4.刪除學(xué)員信息;");
System.out.println("*********************************");
Scanner input = new Scanner(System.in);
int n = 0;
System.out.print("---請(qǐng)輸入選擇:");
n = input.nextInt();
switch(n){
case 1:showStu();break;
case 2:addStu();break;
case 3: updateStu();break;//修改學(xué)員信息
case 4: deleteStu();break;//刪除學(xué)員信息
default:System.out.println("選擇錯(cuò)誤!");break;
}
}
}
總結(jié)
以上是生活随笔為你收集整理的mysql数据库小系统_Mysql数据库基础小实例 学员管理系统菜单的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql索引如何做_5分钟,告诉你My
- 下一篇: mysql自动从另外表取数_你在 Doc