当前位置:首页 > 实验九 通过JDBC方式操作数据库
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) {
共分享92篇相关文档