云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 实验九 通过JDBC方式操作数据库

实验九 通过JDBC方式操作数据库

  • 62 次阅读
  • 3 次下载
  • 2025/5/2 23:27:00

static Connection con=null;

public void SIMS() { }

public void init() { }

static Connection connect() {

try {

Class.forName(\

con = DriverManager.getConnection(\

text1=new JTextField(10); text2=new JTextField(10); text3=new JTextField(10); result=new JTextField(20); button1=new JButton(\追加\button2=new JButton(\查询\button3=new JButton(\删除\button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); add(new JLabel(\学号:\add(text1); add(button1);

add(new JLabel(\姓名:\add(text2); add(button2);

add(new JLabel(\年龄:\add(text3); add(button3); add(result); connect(); init();

setBounds(100,100,350,200); setVisible(true);

setLayout(new FlowLayout());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

} catch (SQLException e) {

e.printStackTrace(); e.printStackTrace();

} catch (ClassNotFoundException e) {

}

public void select() {

try {

Connection con = connect();

Statement statement = con.createStatement(); String sno;

System.out.println(\请输入要查询的学号:\Scanner cin = new Scanner(System.in); sno = cin.next();

String str = \ResultSet rs = statement.executeQuery(str); while(rs.next()){ }

rs.close(); statement.close();

sno = rs.getString(1);

String sname = rs.getString(2); int age = rs.getInt(3);

System.out.println(sno + \

} catch (SQLException e) { }

e.printStackTrace();

}

public void insert() {

try {

Connection con = connect();

Statement statement = con.createStatement(); String sno, sname; int age;

System.out.println(\请输入学号 姓名 年龄\Scanner cin = new Scanner(System.in); sno = cin.next(); sname = cin.next(); age = cin.nextInt();

String str = \

+ age + \

statement.executeUpdate(str); statement.close(); }

return con;

} catch (SQLException e) {

}

}

}

e.printStackTrace();

public void delete() { }

public void actionPerformed(ActionEvent e) { }

if(e.getSource()==button1)

insert(); select(); delete();

if(e.getSource()==button2) if(e.getSource()==button3) try {

Connection con = connect();

Statement statement = con.createStatement(); String sno;

System.out.println(\请输入要删除的学号\Scanner cin = new Scanner(System.in); sno = cin.next();

String str = \ from t1 where sno=\statement.executeUpdate(str); statement.close();

} catch (SQLException e) { }

e.printStackTrace();

package test2; public class Test2 {

} /**

* @param args */

public static void main(String[] args) {

// TODO Auto-generated method stub SIMS s = new SIMS(); //s.setTitle(\

}

实验题3 向student表连续插入5条记录,使用JDBC事务保证插入操作正确性,若若5条记录插入成功,则操作成功;否则回滚插入操作。

详细代码:

package test3;

import java.sql.*;

public class test3 {

} catch (SQLException e) {

try {

System.out.println(\

public void appendFunction(String[][] records) {

String temp = \try {

ps = conn.prepareStatement(temp); ps.clearBatch();

for (int i = 0; i < records.length; i++) { }

ps.executeBatch(); conn.commit();

System.out.println(\

ps.setString(1, records[i][0]); ps.setString(2, records[i][1]);

ps.setInt(3, Integer.parseInt(records[i][2])); ps.addBatch();

public test3() throws SQLException { }

conn = DriverManager.getConnection(\private Connection conn; private PreparedStatement ps; static { }

try { }

Class.forName(\LServerDriver\e.printStackTrace();

} catch (ClassNotFoundException e) {

搜索更多关于: 实验九 通过JDBC方式操作数据库 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

static Connection con=null; public void SIMS() { } public void init() { } static Connection connect() { try { Class.forName(\con = DriverManager.getConnection(\ text1=new JTextField(10); text2=new JTextField(10); text3=new JTextField(10); result=new JTextField

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com