加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sqlserver2000数据库访问

发布时间:2020-12-12 15:18:46 所属栏目:MsSql教程 来源:网络整理
导读:? ?增加数据: ??? Connection con = null; ??? Statement stmt = null; ??? int row = 0; ??? // 获取 GUI 信息 ??? String stuID = stuIDTextField.getText (); ??? String stuName = stuNameTextField.getText (); ??? String stuDepart = (String) depar

?

?增加数据:

??? Connection con = null;

??? Statement stmt = null;

??? int row = 0;

??? //获取GUI信息

??? String stuID = stuIDTextField.getText();

??? String stuName = stuNameTextField.getText();

??? String stuDepart = (String) departComboBox.getSelectedItem();

??? String stuClass = (String) classComboBox.getSelectedItem();

??? //检测

??? if (stuID.equals("") || stuName.equals("")) {

??????? JOptionPane.showMessageDialog(this,"请填写完整");

??????? return;

??? } else {

?

??? try {

?? //注册直连驱动???

?Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

?? //建立连接

?? String url="jdbc:microsoft:sqlserver://localhost:1433;databasename=JDBC_STU";

?con = DriverManager.getConnection(url,"sa","sql2008");

? //创建执行语句,拼接字符串

?? String insertsql = "insert into studentinfo values('" + stuID + "','" + stuName + "','" + stuDepart + "','" + stuClass + "')";

?? stmt = con.createStatement();

?

注意:驱动的加载和jar包的导入

??????? //发送sql语句

??????????? row = stmt.executeUpdate(insertsql);

??????????? if (row != 0) {

??????????????? JOptionPane.showMessageDialog(this,"添加成功");

??????????????? stuIDTextField.setText("");

??????????????? stuNameTextField.setText("");

??????????? } catch(SQLException ex){

??????????? JOptionPane.showMessageDialog(this,"添加失败");

??????????? stuIDTextField.setText("");

??????????? stuNameTextField.setText("");???

??????? } catch (Exception ex) {

??????????? ex.printStackTrace();

??????? } finally {

??????????? try {

??????????????? con.close();//关闭连接

?

??????????? } catch (Exception ex) {

??????????????? ex.printStackTrace();

??????????? }

??????? }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读