当前位置:首页 大学排名 数据库教务管理系统源代码

数据库教务管理系统源代码

发布时间:2023-03-11 00:15:32

#include <stdio.h>

#include <string.h>

#include <windows.h>

struct student

{

long int num

char name[20]

int age

char sex[4]

char b[30]

char p[15]

}

int n=0

struct student stu[100]

struct student *p

void lr()

void ll()

void cx()

void xg()

void sc()

void main()

{

int z

printf("+---------------------------+n")

printf("| 欢迎使用学生信息管理系统 |n")

printf("+---------------------------+n")

printf("提示:为保证您的操作得到保存,请按正常顺序退出系统^_^n")

do

{

printf("nttt--------------------------------n")

printf("ttt+主菜单|n")

printf("ttt--------------------------------n")

printf("ttt+[1]----录入学生信息 |n")

printf("ttt+[2]----浏览学生信息 |n")

printf("ttt+[3]----查询学生信息 |n")

printf("ttt+[4]----删除学生信息 |n")

printf("ttt+[5]----修改学生信息 |n")

printf("ttt+[0]----退出系统 |n")

printf("ttt--------------------------------n")

printf("请输入您的选择:")

scanf("%d", &z)

system("color 2f")

switch(z)

{

case 0 :break

case 1 :lr()break

case 2 :ll()break

case 3 :cx()break

case 4 :sc()break

case 5 :xg()break

default:printf("n无效选项!")

}

}

while(z!= 0)

}

void lr()

{

int y

if(n==0)

p=stu

do

{

printf("--------------------n")

printf("请输入学生的学号:")

scanf("%ld",&p->num)

printf("请输入学生的姓名:")

scanf("%s",p->name)

printf("请输入学生的年龄:")

scanf("%d",&p->age)

printf("请输入学生的性别:")

scanf("%s",p->sex)

printf("请输入学生的地址:")

scanf("%s",p->b)

printf("请输入学生的电话:")

scanf("%s",p->p)

n++

p++

printf("n1.继续输入.n0.输入完毕.n")

printf("请选择:")

scanf("%d",&y)

}

while(y==1)

printf("提示:输入完毕!你一共输入%d个n",n)

}

void ll()

{

int i,j

if(n!=0)

{ printf("学生总数:%dn", n)

printf("学号t姓名t年龄t性别t地址tt电话n")

printf("-----------------------------------------------------n")

for(i=0i<ni++)

printf("%ldt%st%dt%st%stt%sn",stu[i].num,stu[i].name,stu[i].age,stu[i].sex,stu[i].b,stu[i].p)

}

else printf("提示:无学生数据,请输入数据!")

}

void cx()

{

int c

int w,i,j=0

char name[20]

if(n!=0)

{

do{printf("n")

printf("+--------------------+n")

printf("| 按学号查询 请按 1 |n")

printf("| 按姓名查询 请按 2 |n")

printf("| 取消请按 0 |n")

printf("+--------------------+n")

printf("请输入您的选择:")

scanf("%d", &c)

switch(c)

{

case 0:break

case 1:

printf("请输入学生的学号")

scanf("%ld", &w)

printf("n")

for(i=0i<ni++)

if(stu[i].num==w)

{

printf("n学号t姓名t年龄t性别t地址tt电话n")

printf("%ldt%st%dt%st%stt%sn",stu[i].num,stu[i].name,stu[i].age,stu[i].sex,stu[i].b,stu[i].p)

j=1

}

if(j==0)

printf("提示:没有该学生记录,请核对!")

break

case 2:

printf("请输入学生的姓名:")

scanf("%s", name)

printf("n")

for(i=0i<ni++)

if(strcmp(name,stu[i].name)==0)

{

printf("n学号t姓名t年龄t性别t地址tt电话n")

j=1

printf("%ldt%st%dt%st%stt%sn",stu[i].num,stu[i].name,stu[i].age,stu[i].sex,stu[i].b,stu[i].p)

}

if(j==0)

printf("提示:没有该学生记录,请核对!")

break

default:

printf("n提示:无效选项!")

break

}

}while(c!= 0)

}else printf("提示:无学生数据,请输入数据!")

return

}

void xg()

{

long int num

int i,j,c

if(n!=0)

{

printf("请输入您要修改的学生的学号:")

scanf("%ld", &num)

printf("n")

for(i=0i<ni++)

if(stu[i].num==num)

j=i

do{

printf("请选择您要修改的学生的信息内容:n")

printf("+----------------------+n")

printf("| 姓名 请按 1 |n")

printf("| 年龄 请按 2 |n")

printf("| 性别 请按 3 |n")

printf("| 学号 请按 4 |n")

printf("| 地址 请按 5 |n")

printf("| 电话 请按 6 |n")

printf("| 取消 请按 0 |n")

printf("+----------------------+n")

printf("请输入您的选择:")

scanf("%d", &c)

printf("n")

switch(c)

{

case 0:break

case 1:printf("请输入新姓名:")

scanf("%s",stu[j].name)

break

case 2:printf("请输入新年龄:")

scanf("%d",&stu[j].age)

break

case 3:printf("请输入新性别:")

scanf("%s",stu[j].sex)

break

case 4:printf("请输入新学号:")

scanf("%ld",&stu[j].num)

break

case 5:printf("请输入新地址:")

scanf("%s",stu[j].b)

break

case 6:printf("请输入新电话:")

scanf("%s",stu[j].p)

break

default:

printf("n无效选项!")

break

}

}while(c!= 0)

}else printf("提示:无学生数据,请输入数据!")

}

void sc()

{

long int num

int i,j,e

if(n!=0)

{

printf("学号t姓名t年龄t性别t地址tt电话n")

printf("-----------------------------------------------------n")

for(i=0i<ni++)

printf("%ldt%st%dt%st%stt%sn",stu[i].num,stu[i].name,stu[i].age,stu[i].sex,stu[i].b,stu[i].p)

printf("请输入您要删除的学生的学号:")

scanf("%ld", &num)

printf("n")

for(i=0i<ni++)

if(num==stu[i].num)

j=i

if(j!=(n-1))

{

for(e=i-1e<ne++,j++)

{

stu[j].num=stu[j+1].num

strcpy(stu[j].name,stu[j+1].name)

strcpy(stu[j].sex,stu[j+1].sex)

stu[j].age=stu[j+1].age

strcpy(stu[j].b,stu[j+1].b)

strcpy(stu[j].p,stu[j+1].p)

n--p--

}

}else {n--p--}

printf("提示:删除完毕!")

}elseprintf("提示:无学生数据,请输入数据!")

}

(高分)急求连接数据库的JAVA学生信息管理系统源代码

这是我用来交作业的程序.虽然不是自己写的.传上来再说了.

等我自己的可以用了.再传...

package jettang

import java.lang.*

import java.awt.*

import java.awt.event.*

import javax.swing.*

import java.sql.*

//////////////////////////////////////////////

//主类

public class STU {

public STU() {

try {

jbInit()

} catch (Exception ex) {

ex.printStackTrace()

}

}

public static void main(String args[]) {

new Frm_Main()

}

private void jbInit() throws Exception {

}

}

//主框架,数据库

class Frm_Main implements ActionListener { //throws IOException

public static final int EXIT_ON_CLOSE = 0

public Jframe frame

public Container c

public JMenuBar menuBar

public JMenu mainMenu1

public JMenu mainMenu2

public JMenu mainMenu3

public JMenuItem subMenu1[] = new JMenuItem[5]

public JMenuItem subMenu2[] = new JMenuItem[7]

public JMenuItem subMenu3[] = new JMenuItem[2]

JButton toolBarButton[] = new JButton[8]

public JToolBar toolBar

//////////////////////////////////////////////

String strTip[] = {"姓名查询...", "添加信息...",

"删除...", "修改...", "修改确认...",

"添加确认...", "删除确认...", "关于产品信息:)"}

//////////////////////////////////////////////

// String id = new String()

String name = new String()

String sex = new String()

String age = new String()

String classid = new String()

String department = new String()

String call = new String()

//////////////////////////////////////////////

Connection con

ResultSet rs

Statement st

// ResultSetmetaData rsmd

// Vector columnHeads=new Vector()

// Vector rows = new Vector()

// Vector currentRow=new Vector()

///////////////////////////////////////////////

public JLabel classidL = new JLabel("班级")

public JLabel nameL = new JLabel("姓名")

public JLabel sexL = new JLabel("性别")

public JLabel ageL = new JLabel("年龄")

public JLabel departmentL = new JLabel("单位")

public JLabel callL = new JLabel("电话")

public JLabel tittleLabel = new JLabel()

//////////////////////////////////////////////

public JTextField classidT = new JTextField()

public JTextField nameT = new JTextField()

public JTextField sexT = new JTextField()

public JTextField ageT = new JTextField()

public JTextField departmentT = new JTextField()

public JTextField callT = new JTextField()

public Jtextarea textT = new Jtextarea()

//////////////////////////////////////////////

//////////////////////////////////////////////

public Frm_Main() {

/////////////////////////////////////////////////

frame = new Jframe("学籍管理系统V1.0")

c = frame.getContentPane()

c.setLayout(null)

menuBar = new JMenuBar()

toolBar = new JToolBar()

toolBar.setFloatable(false)

frame.setJMenuBar(menuBar)

frame.setResizable(false)

////////////////////////////////////////////////

mainMenu1 = new JMenu("管理")

String str1[] = {"添加", "删除", "查询", " ", "关闭"}

for (int i = 0i <5i++) {

if (i == 3) {

mainMenu1.addSeparator()

} else {

subMenu1[i] = new JMenuItem(str1[i])

subMenu1[i].addActionListener(this)

mainMenu1.add(subMenu1[i])

}

}

menuBar.add(mainMenu1)

/////////////////////////////////////////////////

mainMenu2 = new JMenu("操作")

String str2[] = {"查询", "添加", "删除", "修改",

"提交修改", "提交添加", "提交删除"}

for (int i = 0i <7i++) {

subMenu2[i] = new JMenuItem(str2[i])

subMenu2[i].addActionListener(this)

mainMenu2.add(subMenu2[i])

}

menuBar.add(mainMenu2)

/////////////////////////////////////////////////

mainMenu3 = new JMenu("集体查询")

String str3[] = {"按班级...", "按单位..."}

for (int i = 0i <2i++) {

subMenu3[i] = new JMenuItem(str3[i])

subMenu3[i].addActionListener(this)

mainMenu3.add(subMenu3[i])

}

menuBar.add(mainMenu3)

////////////////////////////////////////////////

String strToolBar[] = {"查询", "添加", "删除", "修改", "提交修改",

"提交添加", "提交删除", "关于"}

for (int i = 0i <8i++) {

toolBarButton[i] = new JButton(strToolBar[i])

toolBarButton[i].setToolTipText(strTip[i])

toolBarButton[i].addActionListener(this)

toolBar.add(toolBarButton[i])

}

toolBar.setLocation(0, 0)

toolBar.setSize(400, 30)

c.add(toolBar)

////////////////////////////////////////////////

classidL.setLocation(35, 40)

classidL.setSize(40, 20)

//classidL.setFont(new Font("",Font.BOLD,12))

c.add(classidL)

classidT.setLocation(90, 40)

classidT.setSize(200, 20)

//classidT.setEnabled(false)

c.add(classidT)

nameL.setLocation(35, 70)

nameL.setSize(40, 20)

c.add(nameL)

nameT.setLocation(90, 70)

nameT.setSize(200, 20)

c.add(nameT)

sexL.setLocation(35, 100)

sexL.setSize(40, 20)

c.add(sexL)

sexT.setLocation(90, 100)

sexT.setSize(200, 20)

c.add(sexT)

ageL.setLocation(35, 130)

ageL.setSize(40, 20)

c.add(ageL)

ageT.setLocation(90, 130)

ageT.setSize(200, 20)

c.add(ageT)

departmentL.setLocation(35, 160)

departmentL.setSize(40, 20)

c.add(departmentL)

departmentT.setLocation(90, 160)

departmentT.setSize(200, 20)

c.add(departmentT)

callL.setLocation(35, 190)

callL.setSize(40, 20)

c.add(callL)

callT.setLocation(90, 190)

callT.setSize(200, 20)

c.add(callT)

/////////////////////////////////////////////////

tittleLabel.setText("----------集体查询结果--------")

tittleLabel.setSize(300, 20)

tittleLabel.setLocation(10, 210)

c.add(tittleLabel)

////////////////////////////////////////////

textT.setSize(400, 300)

textT.setLocation(10, 240)

c.add(textT, 14)

///////////////////////////////////////////////

frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE)

frame.setSize(500, 650)

frame.setLocation(200, 200)

frame.setVisible(true)

conDB()

}

//以上是框架设计

//JDBC

public void conDB() {

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

} catch (ClassNotFoundException e) {

JOptionPane.showMessageDialog(null, "数据库错误")

}

try {

con = DriverManager.getConnection("jdbc:odbc:jettang", "sa", "")

st = con.createStatement()

} catch (SQLException e) {

JOptionPane.showMessageDialog(null, "数据库连接失败")

}

}

//关闭连接

public void closeDB() {

try {

st.close()

con.close()

} catch (SQLException e) {

JOptionPane.showMessageDialog(null, "数据库关闭失败")

}

}

//事件响应

public void actionPerformed(ActionEvent e) {

//////////////////////////////////////////////

if (e.getSource() == toolBarButton[7]) {

JOptionPane.showMessageDialog(null, "当前版本1.0,designed by qyq &wxm")

}

/////////////////////////////////////////////查询(按姓名)

if (e.getSource() == subMenu2[0] || e.getSource() == toolBarButton[0] ||

e.getSource() == subMenu1[2]) {

String idid = JOptionPane.showInputDialog("请输入学生姓名")

if (idid.trim() != "") {

String strSQL = "select * from stuinfo where name ='" + idid +

"'"

try {

rs = st.executeQuery(strSQL)

int count = 0

while (rs.next()) {

classid = rs.getString("classid")

name = rs.getString("name")

department = rs.getString("department")

sex = rs.getString("sex")

age = rs.getString("age")

call = rs.getString("call")

++count

}

if (count == 0) {

JOptionPane.showMessageDialog(null, "无此信息")

} else {

classidT.setText(classid)

nameT.setText(name)

sexT.setText(sex)

ageT.setText(age)

callT.setText(call)

departmentT.setText(department)

}

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "查询失败")

}

}

}

/////////////////////////////////////////////关闭

if (e.getSource() == subMenu1[4]) {

frame.dispose()

closeDB()

}

/////////////////////////////////////////////添加

if (e.getSource() == subMenu1[0] || e.getSource() == subMenu2[1] ||

e.getSource() == toolBarButton[1]) {

JOptionPane.showMessageDialog(null, "请输入你添加的信息再点击提交添加键")

classidT.setEnabled(true)

classidT.setText("")

nameT.setText("")

sexT.setText("")

ageT.setText("")

callT.setText("")

departmentT.setText("")

}

/////////////////////////////////////////////提交添加

if (e.getSource() == toolBarButton[5] || e.getSource() == subMenu2[5]) {

if ((classidT.getText().trim()).equals("") ||

(nameT.getText().trim()).equals("") ||

(sexT.getText().trim()).equals("") ||

(ageT.getText().trim()).equals("") ||

(callT.getText().trim()).equals("") ||

(departmentT.getText().trim()).equals("")) {

JOptionPane.showMessageDialog(null, "请先点击添加键")

} else {

classid = classidT.getText()

name = nameT.getText()

sex = sexT.getText()

age = ageT.getText()

call = callT.getText()

department = departmentT.getText()

String strSQL =

"insert into stuinfo(classid,name,sex,age,call,department) values('" +

classid + "','" +

name + "','" + sex + "','" + age + "','" + call + "','" +

department + "')"

try {

st.executeUpdate(strSQL)

} catch (Exception exx) {

JOptionPane.showMessageDialog(null, "添加失败")

return

}

JOptionPane.showMessageDialog(null, "添加成功")

classidT.setText("")

nameT.setText("")

sexT.setText("")

ageT.setText("")

callT.setText("")

departmentT.setText("")

}

}

////////////////////////////////////////////提交修改

if (e.getSource() == subMenu2[4] || e.getSource() == toolBarButton[4]) {

if ((classidT.getText().trim()).equals("") ||

(nameT.getText().trim()).equals("") ||

(sexT.getText().trim()).equals("")

|| (ageT.getText().trim()).equals("") ||

(callT.getText().trim()).equals("") ||

(departmentT.getText().trim()).equals("")) {

JOptionPane.showMessageDialog(null, "请先点击修改键")

return

} else {

classid = classidT.getText()

name = nameT.getText()

sex = sexT.getText()

age = ageT.getText()

call = callT.getText()

department = departmentT.getText()

String strSQL = "update stuinfo set classid='" + classid +

"',sex='" + sex + "',age='" + age + "',call='" +

call + "',department='" + department + "'" +

"where name='" + name + "'"

try {

st.executeUpdate(strSQL)

} catch (Exception exx) {

JOptionPane.showMessageDialog(null, "修改失败")

return

}

JOptionPane.showMessageDialog(null, "修改成功")

classidT.setText("")

nameT.setText("")

sexT.setText("")

ageT.setText("")

callT.setText("")

departmentT.setText("")

}

}

///////////////////////////////////////////////修改

if (e.getSource() == subMenu2[3] || e.getSource() == toolBarButton[3]) {

String idid = JOptionPane.showInputDialog

("请输入你要修改的学生姓名后点击提交修改键确认")

if (idid.trim() != "") {

String strSQL = "select * from stuinfo where name ='" + idid +

"'"

try {

rs = st.executeQuery(strSQL)

int count = 0

while (rs.next()) {

classid = rs.getString("classid")

name = rs.getString("name")

department = rs.getString("department")

sex = rs.getString("sex")

age = rs.getString("age")

call = rs.getString("call")

++count

}

if (count == 0) {

JOptionPane.showMessageDialog(null, "无此信息")

} else {

classidT.setText(classid)

nameT.setText(name)

sexT.setText(sex)

ageT.setText(age)

callT.setText(call)

departmentT.setText(department)

classidT.setEnabled(false)

}

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "修改失败")

}

}

}

/////////////////////////////////////////////////删除

if (e.getSource() == subMenu2[2] || e.getSource() == toolBarButton[2] ||

e.getSource() == subMenu1[1]) {

String idDel = JOptionPane.showInputDialog

("请输入要删除的学生姓名后点击提交删除键确认")

if (idDel.trim() != "") {

String strSQL = "select * from stuinfo where name ='" + idDel +

"'"

try {

rs = st.executeQuery(strSQL)

int count = 0

while (rs.next()) {

classid = rs.getString("classid")

name = rs.getString("name")

department = rs.getString("department")

sex = rs.getString("sex")

age = rs.getString("age")

call = rs.getString("call")

++count

}

if (count == 0) {

JOptionPane.showMessageDialog(null, "无此信息")

} else {

classidT.setText(classid)

nameT.setText(name)

sexT.setText(sex)

ageT.setText(age)

callT.setText(call)

departmentT.setText(department)

classidT.setEnabled(false)

}

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "删除失败")

}

}

}

//////////////////////////////////////////////////提交删除

if (e.getSource() == toolBarButton[6] ||

e.getSource() == subMenu2[6]) {

if ((classidT.getText().trim()).equals("") ||

(nameT.getText().trim()).equals("") ||

(sexT.getText().trim()).equals("") ||

(ageT.getText().trim()).equals("") ||

(callT.getText().trim()).equals("") ||

(departmentT.getText().trim()).equals("")) {

JOptionPane.showMessageDialog(null, "请先点击删除键")

return

} else {

name = nameT.getText()

String strSQL = "delete from stuinfo where name='" + name +

"'"

try {

st.executeUpdate(strSQL)

} catch (Exception exx) {

JOptionPane.showMessageDialog(null, "删除失败")

return

}

JOptionPane.showMessageDialog(null, "删除成功")

classidT.setText("")

nameT.setText("")

sexT.setText("")

ageT.setText("")

callT.setText("")

departmentT.setText("")

}

}

//////////////////////////////////////////////集体查询(按班级)

if (e.getSource() == subMenu3[0]) {

classidT.setText("")

nameT.setText("")

sexT.setText("")

ageT.setText("")

callT.setText("")

departmentT.setText("")

if ((classidT.getText().trim()).equals("")) {

String idclass = JOptionPane.showInputDialog("请输入班级")

String strSQL = "select * from stuinfo where classid='" +

idclass +

"'"

try {

rs = st.executeQuery(strSQL)

displayres(rs)

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "查询失败")

}

} else {

String idclass = classidT.getText()

String strSQL = "select * from stuinfo where classid='" +

idclass +

"'"

try {

rs = st.executeQuery(strSQL)

displayres(rs)

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "查询失败")

}

}

}

////////////////////////////按单位

if (e.getSource() == subMenu3[1]) {

classidT.setText("")

nameT.setText("")

sexT.setText("")

ageT.setText("")

callT.setText("")

departmentT.setText("")

if ((departmentT.getText().trim()).equals("")) {

String idclass = JOptionPane.showInputDialog("请输入单位")

String strSQL = "select * from stuinfo where department='" +

idclass +

"'"

try {

rs = st.executeQuery(strSQL)

displayres(rs)

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "查询失败")

}

} else {

String idclass = departmentT.getText()

String strSQL = "select * from stuinfo where department='" +

idclass +

"'"

try {

rs = st.executeQuery(strSQL)

displayres(rs)

} catch (Exception ex) {

JOptionPane.showMessageDialog(null, "查询失败")

}

}

}

}

public void displayres(ResultSet rs1) throws SQLException {

//定位到达第一条记录

boolean moreRecords = rs1.next()

//如果没有记录,则提示一条消息

if (!moreRecords) {

JOptionPane.showMessageDialog(null, "无此记录")

return

}

try {

//获取数据

getNextRow(rs1)

//刷新Table

c.validate()

}

catch (SQLException sqlex) {

sqlex.printStackTrace()

}

}

public void getNextRow(ResultSet rs2) throws

SQLException {

int count = 1

while (rs2.next()) {

name = " 姓名:" + rs2.getString(1)

age = " 年龄:" + rs2.getString(2) + " 单位:"

department = rs2.getString(3) + "n"

textT.append(count + "." + name + age + department)

count++

}

}

}

急求java学生信息管理系统源代码,带有连接数据库的,万分感谢

数据库连接(Connection)

数据库连接

获取数据库连接有两种方法,一种是通过驱动程序管理器DriverManager类,另一种则是使用DataSource接口。这两种方法都提供了了一个getConnection方法,用户可以在程序中对它们进行相应处理后调用这个方法来返回数据库连接。

• DriverManager类

• DataSource接口

• Connection接口

• JDBC URL

jdbc:<subprotocol>:<subname>

• 驱动程序注册方法

(1)调用Class.forName方法

(2)设置jdbc.drivers系统属性

• DriverManager方法

DriverManager类中的所有方法都是静态方法,所以使用DriverManager类的方法时,不必生成实例。

DriverManager

• getConnection方法

作用是用于获取数据库连接,原型如下:

public static Connection getConnection(String url)

throws SQLException;

public static Connection getConnection(String url, String user, String password)

throws SQLException

public static Connection getConnection(String url, Properties info)

throws SQLException

• 使用DriverManager的getConnetion方法

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

Connection conn = DriverManager.getConnection

("jdbc:odbc:sqlserver", "sa", "sa")

• 使用设置jdbc.drivers系统属性的方法

java -Djdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver test.java

DataSource 接口

……

//从上下文中查找数据源,并获取数据库连接

Context ctx = new InitialContext()

DataSource ds = (DataSource) ctx.lookup("sqlserver")

Connection conn = ds.getConnection()

//查询数据库中所有记录

Statement stmt = conn.createStatement()

ResultSet rs = stmt.executeQuery("SELECT * FROM student")

……

Connection 接口

Connection接口代表了已经建立的数据库连接,它是整个JDBC的核心内容。Connnection接口中的方法按照它们所实现的功能,可以分为三类:

• 生成数据库语句

• 管理数据库事务

• 获取数据库信息

生成数据库语句

JDBC将数据库语句分成三种类型 :

• 生成Statement 语句 :

Connection.createStatement()

• 生成PreparedStatement 语句 :

Connection. prepareStatement()

• 生成CallableStatement 语句 :

Connection. prepareCall ()

管理数据库事务

• 默认情况下,JDBC将一条数据库语句视为一个完整的事务。可以关掉默认事务管理:

public void setAutoCommit(Boolean autoCommit) throws SQLException;

将autoCommit的值设置为false,就关掉了自动事务管理模式

• 在执行完事务后,应提交事务:

public void commit() throws SQLException;

• 可以取消事务:

public void rollback() throws SQLException;

第二讲 第四部分

数据库语句

数据库语句

JDBC数据库语句共有三种类型:

• Statement:

Statement语句主要用于嵌入一般的SQL语句,包括查询、更新、插入和删除等等。

• PreparedStatement:

PreparedStatement语句称为准备语句,它是将SQL语句中的某些参数暂不指定,而等到执行时在统一指定。

• CallableStatement:

CallableStatement用于执行数据库的存储过程。

Statement 语句

• executeQuery方法

• executeUpdate方法

• execute方法

• close方法

executeQuery方法

• executeQuery方法主要用于执行产生单个结果集的SQL查询语句(QL),即SELECT语句。executeQuery方法的原型如下所示:

• public ResultSet executeQuery(String sql) throws SQLException

executeUpdate方法

• executeUpdate方法主要用于执行 INSERT、UPDATE、DELETE语句,即SQL的数据操作语句(DML)

• executeUpdate方法也可以执行类似于CREATE TABLE和DROP TABLE语句的SQL数据定义语言(DDL)语句

• executeUpdate方法的返回值是一个整数,指示受影响的行数(即更新计数)。而对于CREATE TABLE 或 DROP TABLE等并不操作特定行的语句,executeUpdate的返回值总为零。

execute方法

execute方法用于执行:

• 返回多个结果集

• 多个更新计数

• 或二者组合的语句

execute方法

• 返回多个结果集:首先要调用getResultSet方法获得第一个结果集,然后调用适当的getter方法获取其中的值。要获得第二个结果集,需要先调用getMoreResults方法,然后再调用getResultSet方法。

• 返回多个更新计数:首先要调用getUpdateCount方法获得第一更新计数。然后调用getMoreResults,并再次调用getUpdateCount获得后面的更新计数。

• 不知道返回内容:如果结果是ResultSet对象,则execute方法返回true;如果结果是int类型,则意味着结果是更新计数或执行的语句是DDL命令。

execute方法

为了说明如果处理execute方法返回的结果,下面举一个代码例子:

stmt.execute(query)

while (true) {

int row = stmt.getUpdateCount()

//如果是更新计数

if (row >0) {

System.out.println("更新的行数是:" + row)

stmt.getMoreResults()

continue

}

execute方法

//如果是DDL命令或0个更新

if (row == 0) {

System.out.println("没有更新,或SQL语句是一条DDL语句!")

stmt.getMoreResults()

continue

}

//如果是一个结果集

ResultSet rs = stmt.getResultSet

if (rs != null) {

while (rs.next()) {

// 处理结果集

. . .

}

stmt.getMoreResults()

continue

}

break

}

PreparedStatement 语句

登录一个网站或BBS时 :

• 使用Statement语句

Statement stmt = conn.createStatement()

ResultSet rs = stmt.executeQuery

(“SELECT password FROM userinfo

WHERE id=userId")

• 使用PreparedStatement语句

PreparedStatement pstmt=conn.prepareStatement

(“SELECT password FROM userinfo

WHERE id=?")

pstmt.setString(1, userId)

PreparedStatement语句

• 常用的setter方法

public void setBoolean(int parameterIndex, boolean x) throws SQLException

public void setByte(int parameterIndex, byte x) throws SQLException

public void setShort(int parameterIndex, short x) throws SQLException

public void setInt(int parameterIndex,int x) throws SQLException

public void setLong(int parameterIndex, long x) throws SQLException

public void setFloat(int parameterIndex, float x) throws SQLException

public void setDouble(int parameterIndex, double x) throws SQLException

public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException

public void setString(int parameterIndex, String x) throws SQLException

public void setBytes(int parameterIndex, byte[] x) throws SQLException

public void setDate(int parameterIndex, Date x) throws SQLException

public void setTime(int parameterIndex, Time x) hrows SQLException

public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException

PreparedStatement语句

• PreparedStatement接口是由Statement接口扩展而来的,重写了executeQuery方法、executeUpdate方法和execute 方法

• public ResultSet executeQuery() throws SQLException

• public int executeUpdate() throws SQLException

• public boolean execute() throws SQLException

CallableStatement语句

• CallableStatement语句是由Connection接口的prepareCall方法创建的,创建时需要传入字符串参数,参数的形式为:

• {call procedure_name[(?, ?, ...)]}

• {? = call procedure_name[(?, ?, ...)]}

• {call procedure_name}

CallableStatement语句

• 其中的问号是参数占位符,参数共有两种:

• IN参数

• OUT参数

• IN参数使用setter方法来设置

• OUT参数则使用registerOutParameter方法来设置

CallableStatement 语句

CallableStatement cstmt = con.prepareCall

("{call getTestData(?, ?)}")

cstmt.registerOutParameter

(1, java.sql.Types.TINYINT)

cstmt.registerOutParameter

(2, java.sql.Types.DECIMAL, 3)

cstmt.executeQuery()

byte x = cstmt.getByte(1)

java.math.BigDecimal n =

cstmt.getBigDecimal(2, 3)

第二讲 第五部分

结 果 集

结果集

• JDBC为了方便处理查询结果,又专门定义了一个接口,这个接口就是ResultSet接口。ResultSet接口提供了可以访问数据库查询结果的方法,通常称这个接口所指向的对象为结果集。

• 有两种方法得到结果集,一种是直接执行查询语句,将结果存储在结果集对象上;另一种是不存储返回结果,而在需要时调用数据库语句的getResultSet方法来返回结果集

结果集

• 结果集指针

由于返回的结果集可能包含多条数据记录,因此ResultSet 接口提供了对结果集的所有数据记录轮询的方法。结果集自动维护了一个指向当前数据记录的指针,初始时这个指针是指向第一行的前一个位置。 next 方法就是用于向前移动指针的

结果集

• 结果集属性

默认情况下,结果集是一个不可更新集,并且结果集的指针也只能向前移动。也就是说,在得到了一个结果集之后,用户只能按照从第一条记录到最后一条记录的顺序依次向后读取,而不能跳到任意条记录上,也不能返回到前面的记录。不仅如此,结果集的这种轮询只能进行一次,而不能再将指针重置到初始位置进行多次轮询

结果集

• 结果集属性

类型

并发性

有效性

• 属性的设置是在生成数据库语句时通过向生成方法传入相应的参数设定的,而当结果集已经返回时就不能够再改变它的属性了。

结果集生成Statement语句共有三种方法

public Statement createStatement() throws SQLException

public Statement createStatement

(int resultSetType, int resultSetConcurrency)

throws SQLException

public Statement createStatement

(int resultSetType, int resultSetConcurrency,

int resultSetHoldability)

throws SQLException

结果集

• 生成PreparedStatement语句共有六种方法

public PreparedStatement prepareStatement(String sql) throws SQLException

public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)

throws SQLException

public PreparedStatement prepareStatement(String sql, int[] columnIndexes)

throws SQLException

public PreparedStatement prepareStatement(String sql, int resultSetType,

int resultSetConcurrency)

throws SQLException

public PreparedStatement prepareStatement(String sql, int resultSetType,

int resultSetConcurrency,

int resultSetHoldability)

throws SQLException

public PreparedStatement prepareStatement(String sql. String[] columnNames)

throws SQLException

结果集

• 生成CallableStatement语句共有三种方法

public CallableStatement prepareCall(String sql)

throws SQLException

public CallableStatement prepareCall

(String sql, int resultSetType,

int resultSetConcurrency)

throws SQLException

public CallableStatement prepareCall

(String sql, int resultSetType,

int resultSetConcurrency,

int resultSetHoldability)

throws SQLException

结果集

结果集类型

• 结果集的类型共有三种,TYPE_FORWARD_ONLY类型的结果集只能向前移动指针,而TYPE_SCROLL_INSENSITIVE类型和TYPE_SCROLL_SENSITIVE类型的结果集则可以任意移动指针。后两种类型的区别在于,前者对来自其它处的修改不敏感(静态),而后者则对于别人的修改敏感(动态视图)。

结果集

结果集类型

• 对于可以任意移动指针的结果集,可以用来移动指针的方法包括:

• next 和previous :

• absolute 和relative :参数可正可负

• afterLast 、beforeFirst 、last 和first :

结果集

结果集并发性

• 结果集的并发性共有两种,CONCUR_READ_ONLY的结果集是只读而不可更新的;而CONCUR_UPDATABLE的结果集则是可以通过update方法进行更新的。

• ResultSet接口提供了一组update方法,用于更新结果集中的数据。这些方法与PreparedStatement接口中定义的setter方法一样,也是与类型相对应的。所有的update方法都以update开头 。

• 所有的update方法都有两个参数,第一个参数用于指定更新的列,它可以是列名称也可以是列的序号;第二个参数则表示将要更新列的值。

结果集

结果集并发性

• Statement stmt = conn.createStatement

•(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)

•ResultSet rs = stmt.executeQuery("SELECT * FROM student " +

• "WHERE grade=2 AND math>60 AND physics>60 AND " +

• "chemistry>60 AND english>60 AND chinese>60")

•while(rs.next()){

•rs.updateString("grade", "3")

•rs.updateRow()

•}

结果集

结果集有效性

• 结果集的有效性是指在调用了Connection 接口的commit 方法后,结果集是否自动关闭。所以它只有两个可选值,即HOLD_CURSORS_OVER_COMMIT 和CLOSE_CURSORS_AT_COMMIT 。前者表示调用commit 方法之后,结果集不关闭;而后者则表示关闭结果集。

结果结果集

• 结果集的getter方法

ResultSet接口还提供了一组getter方法,用于返回当前记录的属性值。它们都是以get开头的,后接数据类型。比如,如果要返回一个float类型的列值,则应调用getFloat方法。每一种类型的getter方法都有两种形式,它们的名称相同而参数不同。这两种形式的getter方法都只有一个参数,第一种形式的getter方法参数是String类型的,用于指定列的名称;另外一种形式的getter方法参数则是int类型的,用于指定列的序号。

import java.awt.BorderLayout

import java.awt.CardLayout

import java.awt.Container

import java.awt.Font

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.Icon

import javax.swing.ImageIcon

import javax.swing.JButton

import javax.swing.Jframe

import javax.swing.JLabel

import javax.swing.JMenu

import javax.swing.JMenuBar

import javax.swing.JMenuItem

import javax.swing.JPanel

import javax.swing.JToolBar

import javax.swing.SwingConstants

public class Mainframe extends Jframe implements ActionListener{

InsertPanel ip = null

SelectPanel sp = null

JPanel pframe

JButton jb1,jb2,jb3

JMenuItem jm11,jm21,jm22,jm23,jm31,jm32,jm41,jm42

CardLayout clayout

public Mainframe(String s){

super(s)

JMenuBar mb = new JMenuBar()

this.setJMenuBar(mb)

JMenu m1 = new JMenu("系统")

JMenu m2 = new JMenu("基本信息")

JMenu m3 = new JMenu("成绩")

JMenu m4 = new JMenu("奖惩")

mb.add(m1)

mb.add(m2)

mb.add(m3)

mb.add(m4)

jm11 = new JMenuItem("退出系统")

jm21 = new JMenuItem("输入")

jm22 = new JMenuItem("查询")

jm23 = new JMenuItem("更改")

jm31 = new JMenuItem("输入成绩")

jm32 = new JMenuItem("查询成绩")

jm41 = new JMenuItem("奖励")

jm42 = new JMenuItem("处分")

m1.add(jm11)

m2.add(jm21)

m2.add(jm22)

m2.add(jm23)

m3.add(jm31)

m3.add(jm32)

m4.add(jm41)

m4.add(jm42)

Icon i1 = new ImageIcon()

Icon i2 = new ImageIcon()

Icon i3 = new ImageIcon()

jb1 = new JButton(i1)

jb1.setToolTipText("输入")

jb2 = new JButton(i2)

jb2.setToolTipText("查询")

jb3 = new JButton(i3)

jb3.setToolTipText("退出")

JToolBar tb = new JToolBar("系统工具")

tb.add(jb1)

tb.add(jb2)

tb.add(jb3)

add(tb,BorderLayout.NORTH)

jm11.addActionListener(this)

jm21.addActionListener(this)

jm22.addActionListener(this)

jb1.addActionListener(this)

jb2.addActionListener(this)

jb3.addActionListener(this)

clayout = new CardLayout()

pframe = new JPanel(clayout)

add(pframe)

JPanel mainp = new JPanel(new BorderLayout())

JLabel mainl = new JLabel("学生信息管理平台",SwingConstants.CENTER)

mainl.setFont(new Font("serif",Font.BOLD,30))

mainp.add(mainl)

pframe.add(mainp,"main")

clayout.show(pframe, "main")

}

public void actionPerformed(ActionEvent e){

if(e.getSource() == jm21 || e.getSource() == jb1){

if(ip == null){

ip= new InsertPanel()

pframe.add(ip,"insert")

}

clayout.show(pframe, "insert")

this.setTitle("输入学生信息")

}

else if(e.getSource() == jm22 || e.getSource() == jb2){

if(sp == null){

sp= new SelectPanel()

pframe.add(sp,"select")

}

clayout.show(pframe, "select")

this.setTitle("查询学生信息")

}

else if(e.getSource() == jm11 || e.getSource() == jb3){

System.exit(0)

}

}

}

第二个:

import javax.swing.Jframe

public class MainTest {

public static void main(String [] args){

Mainframe f = new Mainframe("学生信息管理平台")

f.setSize(400,300)

f.setLocation(350,250)

f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE)

f.setVisible(true)

}

}

第二个:

import java.sql.Connection

import java.sql.DriverManager

public class MySQLConnection {

static Connection getCon(){

Connection con = null

try{

Class.forName("com.mysql.jdbc.Driver")

con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","123")

}

catch(Exception e){

System.out.println("建立数据库连接遇到异常!")

}

return con

}

}

第四个:

import java.awt.BorderLayout

import java.awt.GridLayout

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JButton

import javax.swing.JLabel

import javax.swing.JOptionPane

import javax.swing.JPanel

import javax.swing.JTextField

import javax.swing.SwingConstants

public class SelectPanel extends JPanel implements ActionListener{

JButton jb

JTextField jt

JTextField jt1,jt2,jt3,jt4

public SelectPanel(){

JLabel jl = new JLabel("请输入学号:",SwingConstants.CENTER)

jt = new JTextField()

jb = new JButton("确定")

JPanel jp1 = new JPanel(new GridLayout(1,3))

jp1.add(jl)

jp1.add(jt)

jp1.add(jb)

JLabel j1,j2,j3,j4

j1 = new JLabel("学号:",SwingConstants.CENTER)

j2 = new JLabel("姓名:",SwingConstants.CENTER)

j3 = new JLabel("性别:",SwingConstants.CENTER)

j4 = new JLabel("年龄:",SwingConstants.CENTER)

jt1 = new JTextField(6)

jt1.setEditable(false)

jt2 = new JTextField(6)

jt2.setEditable(false)

jt3 = new JTextField(6)

jt3.setEditable(false)

jt4 = new JTextField(6)

jt4.setEditable(false)

JPanel jp2 = new JPanel(new BorderLayout())

JPanel jp3 = new JPanel(new GridLayout(4,2))

jp2.add(new JLabel(""),BorderLayout.NORTH)

jp3.add(j1)

jp3.add(jt1)

jp3.add(j2)

jp3.add(jt2)

jp3.add(j3)

jp3.add(jt3)

jp3.add(j4)

jp3.add(jt4)

jp2.add(jp3)

this.setLayout(new BorderLayout())

add(jp1,BorderLayout.NORTH)

add(jp2)

jb.addActionListener(this)

}

public void actionPerformed(ActionEvent e){

if(e.getSource() == jb){

String stuNo = jt.getText().trim()

Student s = new Student()

boolean b = true

try{

b = s.selectByStuNo(stuNo)

}

catch(Exception ex){

System.out.println("查询学生信息遇到异常!")

}

if(b){

jt1.setText(s.getStuNo())

jt2.setText(s.getName())

jt3.setText(s.getGender())

int a = s.getAge()

Integer i = new Integer(a)

jt4.setText(i.toString())

}

else{

JOptionPane.showMessageDialog(null, "无此学生!")

}

}

}

}

第五个:

import javax.swing.Jframe

public class SelectTest {

public static void main(String [] args){

Jframe f = new Jframe("查询学生信息")

SelectPanel p = new SelectPanel()

f.add(p)

f.setSize(400,300)

f.setLocation(300,250)

f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE)

f.setVisible(true)

}

}

第六个:

import java.sql.Connection

import java.sql.ResultSet

import java.sql.Statement

public class Student {

String stuNo

String name

String gender

int age

public Student(){}

public Student(String stuNo,String name,String gender, int age){

this.stuNo = stuNo

this.name = name

this.gender = gender

this.age = age

}

public String getStuNo(){

return stuNo

}

public void setStuNo(String stuNo){

this.stuNo = stuNo

}

public String getName(){

return name

}

public void setName(String name){

this.name = name

}

public String getGender(){

return gender

}

public void setGender(String gender){

this.gender = gender

}

public int getAge(){

return age

}

public void setAge(int age){

this.age = age

}

public boolean insertStudent(){

boolean b = true

try{

Connection con = MySQLConnection.getCon()

Statement statement = con.createStatement()

String sql = "insert into student values('" + stuNo + "','" + name +"','" + gender + "'," + age + ")"

sql = new String(sql.getBytes("gb2312"),"ISO8859_1")

statement.executeUpdate(sql)

con.close()

}

catch(Exception e){

b = false

System.out.println("插入数据库遇到异常!")

}

return b

}

public boolean selectByStuNo(String stuNo)throws Exception{

boolean b = true

Connection con = MySQLConnection.getCon()

Statement statement = con.createStatement()

String sql = "select * from student where stuNo =" + stuNo

ResultSet rs = statement.executeQuery(sql)

if(rs != null &&rs.next()){

String no = rs.getString(1)

this.setStuNo(no)

String n = rs.getString(2)

n = new String(n.getBytes("ISO8859_1"),"gb2312")

this.setName(n)

String g = rs.getString(3)

g = new String (g.getBytes("ISO8859_1"),"gb2312")

this.setGender(g)

this.setAge(rs.getInt(4))

b = true

}

rs.close()

statement.close()

con.close()

return b

}

}

数据库你自己弄吧,我没时间弄了!初学得多动手哦

以上就是关于跪求c语言教务管理系统源代码全部的内容,如果了解更多相关内容,可以关注醉学网,你们的支持是我们更新的动力!

温馨提示:
本文【数据库教务管理系统源代码】由作者 院校教务系统指导 转载提供。 该文观点仅代表作者本人, 自学教育网 信息发布平台,仅提供信息存储空间服务, 若存在侵权问题,请及时联系管理员或作者进行删除。
(c)2008-2025 自学教育网 All Rights Reserved 汕头市灵创科技有限公司
粤ICP备2024240640号-6