第一范文网 - 专业文章范例文档资料分享平台

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

来源:用户分享 时间:2026/7/31 6:16:36 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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方式操作数据库 的文档
实验九 通过JDBC方式操作数据库.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8j2c311wba0zn011pbau_2.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top