xml系列之数据库中数据的导入导出

举报
yd_273762914 发表于 2020/12/03 00:05:45 2020/12/03
【摘要】  这是我一个晚上做出来的,因为要去做其他的项目,所以只实现了对特定数据库的xml操作,不过我觉得这是学习xml挺不错的参考代码和文档 使用说明: 要先导入xml.sql数据库,可以用navicat导入,然后运行java项目就可以,这是java+mysql数据库实现的程序,仅供参考互相学习     实验前准备:   新建一个Java工程,工程名称为xmlDe...



这是我一个晚上做出来的,因为要去做其他的项目,所以只实现了对特定数据库的xml操作,不过我觉得这是学习xml挺不错的参考代码和文档

使用说明:

要先导入xml.sql数据库,可以用navicat导入,然后运行java项目就可以,这是java+mysql数据库实现的程序,仅供参考互相学习

 

 

实验前准备:

 

新建一个Java工程,工程名称为xmlDemo,文件目录如图所示:

 

 

 

 

 

 

 

 

src

 

frame包:存放java的界面类。IndexFrame是索引界面类,ImportFrame是导入界面类,ExportFrame是导出界面类;

 

service包:存放java的Service类。DBService是实现数据库操作的Service类,DBToXmlService是实现从数据库导出xml文件的Service类,XmlToDBService是实现从xml文件导入数据库的Service类;

 

utils包:存放java的工具类。DBConnectionUtil是数据库连接的工具类;

 

 

libs

 

dom4j-1.6.1.jar:实现XML读取相关操作的价包;

 

mysql-connector-5.1.8.jar:实现连接MySql数据库的价包;

 

IndexFrame.java:

 

 


  
  1. package com.xmlDemo.frame;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Image;
  5. import java.awt.Toolkit;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.JFrame;
  9. import javax.swing.JMenu;
  10. import javax.swing.JMenuBar;
  11. import javax.swing.JMenuItem;
  12. /**
  13. *项目名称:xml读取转换工具
  14. *类名:IndexJFrame
  15. *类描述:主界面类
  16. *创建人:马增群
  17. *修改备注:
  18. *@version 1.0.0
  19. */
  20. public class IndexFrame extends JFrame{
  21. /**
  22. *
  23. */
  24. private static final long serialVersionUID = 1L;
  25. private JMenuBar menuBar=null;
  26. private JMenu fileMenu=null;
  27. private JMenu helpMenu=null;
  28. private JMenuItem existMenuItem=null;
  29. private JMenuItem importMenuItem=null;
  30. private JMenuItem exportMenuItem=null;
  31. private JMenuItem about=null;
  32. private JMenuItem contact=null;
  33. private JMenuItem introduce=null;
  34. private final static String BASEURL="../xmlDemo/images/";
  35. //构造函数,用于初始
  36. private String arrs2[];
  37. public static void main(String[] args) {
  38. new IndexFrame();
  39. }
  40. public IndexFrame(){
  41. setTitle("xml转换工具");
  42. Image image=Toolkit.getDefaultToolkit().getImage(BASEURL+"logo.png");
  43. setIconImage(image);
  44. setLocationRelativeTo(null);
  45. createMenuBar();
  46. /**/
  47. setJMenuBar(menuBar);
  48. //getContentPane().add("Center",splitPane);
  49. //设置JFrame的属性
  50. setResizable(false);//设置不可以改变大小
  51. pack();//自动调整
  52. setSize(400,600);
  53. //setSize(bg.getIconWidth(), bg.getIconHeight());
  54. //设置运行时窗口的位置
  55. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  56. Dimension frameSize = getSize();
  57. if (frameSize.height > screenSize.height) {
  58. frameSize.height = screenSize.height;
  59. }
  60. if (frameSize.width > screenSize.width) {
  61. frameSize.width = screenSize.width;
  62. }
  63. setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
  64. setVisible(true);
  65. }
  66. /**
  67. * 方法说明:创建菜单栏
  68. */
  69. public void createMenuBar(){
  70. menuBar=new JMenuBar();
  71. menuBar.setBackground(new Color(197,228,251));
  72. fileMenu = new JMenu("文件");
  73. helpMenu=new JMenu("帮助");
  74. //ImageIcon conImage=new ImageIcon(BASEURL+"contact.png");
  75. contact=new JMenuItem("联系");
  76. about=new JMenuItem("关于");
  77. introduce=new JMenuItem("说明");
  78. exportMenuItem = new JMenuItem("xml导出");
  79. exportMenuItem.addActionListener(new ActionListener() {
  80. @Override
  81. public void actionPerformed(ActionEvent e) {
  82. // TODO Auto-generated method stub
  83. try {
  84. new ExportFrame();
  85. } catch (Exception e1) {
  86. // TODO Auto-generated catch block
  87. e1.printStackTrace();
  88. }
  89. }
  90. });
  91. importMenuItem = new JMenuItem("xml导入");
  92. importMenuItem.addActionListener(new ActionListener() {
  93. @Override
  94. public void actionPerformed(ActionEvent e) {
  95. // TODO Auto-generated method stub
  96. new ImportFrame();
  97. }
  98. });
  99. existMenuItem = new JMenuItem("退出软件");
  100. helpMenu.add(contact);
  101. helpMenu.add(about);
  102. helpMenu.add(introduce);
  103. fileMenu.add(exportMenuItem);
  104. fileMenu.add(importMenuItem);
  105. fileMenu.add(existMenuItem);
  106. menuBar.add(fileMenu);
  107. menuBar.add(helpMenu);
  108. }
  109. }

 

 

 

 

ImportFrame.java:

 

 


  
  1. package com.xmlDemo.frame;
  2. import java.awt.Dimension;
  3. import java.awt.FlowLayout;
  4. import java.awt.Image;
  5. import java.awt.Toolkit;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.ItemEvent;
  9. import java.awt.event.ItemListener;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14. import java.util.List;
  15. import javax.swing.DefaultComboBoxModel;
  16. import javax.swing.JButton;
  17. import javax.swing.JComboBox;
  18. import javax.swing.JFileChooser;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.JPanel;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.JTextArea;
  25. import com.xmlDemo.service.DBService;
  26. import com.xmlDemo.service.XmlToDBService;
  27. public class ImportFrame extends JFrame implements ItemListener{
  28. /**
  29. *
  30. */
  31. private static final long serialVersionUID = 1L;
  32. private String filePath;
  33. private final static String BASEURL="../xmlDemo/images/";
  34. private JComboBox comboBox;
  35. private List<String> list;
  36. private String[] arrs = {};
  37. private String dbName ="xml";
  38. public ImportFrame(){
  39. JFileChooser fileChooser=new JFileChooser("打开文件");
  40. int isOpen=fileChooser.showOpenDialog(null);
  41. fileChooser.setDialogTitle("打开文件");
  42. if(isOpen==JFileChooser.APPROVE_OPTION){
  43. filePath = fileChooser.getSelectedFile().getPath();
  44. //final JDialog dialog=new JDialog();
  45. Image image=Toolkit.getDefaultToolkit().getImage(BASEURL+"logo.png");
  46. setIconImage(image);
  47. setTitle("导入信息");
  48. JPanel p1=new JPanel();
  49. JPanel p2=new JPanel();
  50. JTextArea textArea=new JTextArea(60,60);
  51. textArea.setText(readFromFile(filePath));
  52. JScrollPane scrollPanel=new JScrollPane(textArea);
  53. scrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  54. scrollPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  55. scrollPanel.getViewport().add(textArea);
  56. scrollPanel.getViewport().setPreferredSize(new Dimension(700,570));
  57. JButton yes=new JButton("导入");
  58. JButton no=new JButton("取消");
  59. yes.addActionListener(new ActionListener() {
  60. @Override
  61. public void actionPerformed(ActionEvent e) {
  62. try {
  63. if(dbName.equals("xml")){
  64. new XmlToDBService().importDataIntoDB(filePath,dbName);
  65. }else{
  66. JOptionPane.showConfirmDialog(null, "当前版本只支持特定数据库","温馨提示",JOptionPane.YES_NO_OPTION);
  67. }
  68. } catch (Exception e1) {
  69. e1.printStackTrace();
  70. }
  71. }
  72. });
  73. no.addActionListener(new ActionListener() {
  74. @Override
  75. public void actionPerformed(ActionEvent e) {
  76. setVisible(false);
  77. }
  78. });
  79. JLabel label = new JLabel("数据库:");
  80. try {
  81. list = new DBService().getAllDatabases();
  82. } catch (Exception e1) {
  83. // TODO Auto-generated catch block
  84. e1.printStackTrace();
  85. }
  86. arrs = new String[list.size()];
  87. for(int i = 0; i < list.size(); i++){
  88. arrs[i] = list.get(i);
  89. }
  90. comboBox = new JComboBox(arrs);
  91. comboBox.setSelectedItem(dbName);
  92. comboBox.addItemListener(this);
  93. p1.add(scrollPanel);
  94. p2.setLayout(new FlowLayout(FlowLayout.LEFT));
  95. p2.add(label);
  96. p2.add(comboBox);
  97. p2.add(yes);
  98. p2.add(no);
  99. add("Center",p1);
  100. add("South",p2);
  101. setVisible(true);
  102. setSize(800,700);
  103. setLocation(100,100);
  104. }
  105. }
  106. public String readFromFile(String path){
  107. File file=new File(path);
  108. String s=null;
  109. try {
  110. FileInputStream fin=new FileInputStream(file);
  111. int length=fin.available();
  112. byte arr[]=new byte[length];
  113. int len=fin.read(arr);
  114. s=new String(arr,0,len);
  115. } catch (FileNotFoundException e) {
  116. e.printStackTrace();
  117. } catch (IOException e) {
  118. e.printStackTrace();
  119. }
  120. return s;
  121. }
  122. @Override
  123. public void itemStateChanged(ItemEvent e) {
  124. // TODO Auto-generated method stub
  125. if(e.getStateChange() == ItemEvent.SELECTED){
  126. dbName = "" + e.getItem();
  127. }
  128. }
  129. }

 

 

 

 

 

 

ExportFrame.java:

 


  
  1. package com.xmlDemo.frame;
  2. import java.awt.Dimension;
  3. import java.awt.FlowLayout;
  4. import java.awt.Image;
  5. import java.awt.Toolkit;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.ItemEvent;
  9. import java.awt.event.ItemListener;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14. import java.util.List;
  15. import javax.swing.DefaultComboBoxModel;
  16. import javax.swing.JButton;
  17. import javax.swing.JComboBox;
  18. import javax.swing.JFileChooser;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.JPanel;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.JTextArea;
  25. import com.xmlDemo.service.DBService;
  26. import com.xmlDemo.service.XmlToDBService;
  27. public class ImportFrame extends JFrame implements ItemListener{
  28. /**
  29. *
  30. */
  31. private static final long serialVersionUID = 1L;
  32. private String filePath;
  33. private final static String BASEURL="../xmlDemo/images/";
  34. private JComboBox comboBox;
  35. private List<String> list;
  36. private String[] arrs = {};
  37. private String dbName ="xml";
  38. public ImportFrame(){
  39. JFileChooser fileChooser=new JFileChooser("打开文件");
  40. int isOpen=fileChooser.showOpenDialog(null);
  41. fileChooser.setDialogTitle("打开文件");
  42. if(isOpen==JFileChooser.APPROVE_OPTION){
  43. filePath = fileChooser.getSelectedFile().getPath();
  44. //final JDialog dialog=new JDialog();
  45. Image image=Toolkit.getDefaultToolkit().getImage(BASEURL+"logo.png");
  46. setIconImage(image);
  47. setTitle("导入信息");
  48. JPanel p1=new JPanel();
  49. JPanel p2=new JPanel();
  50. JTextArea textArea=new JTextArea(60,60);
  51. textArea.setText(readFromFile(filePath));
  52. JScrollPane scrollPanel=new JScrollPane(textArea);
  53. scrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  54. scrollPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  55. scrollPanel.getViewport().add(textArea);
  56. scrollPanel.getViewport().setPreferredSize(new Dimension(700,570));
  57. JButton yes=new JButton("导入");
  58. JButton no=new JButton("取消");
  59. yes.addActionListener(new ActionListener() {
  60. @Override
  61. public void actionPerformed(ActionEvent e) {
  62. try {
  63. if(dbName.equals("xml")){
  64. new XmlToDBService().importDataIntoDB(filePath,dbName);
  65. }else{
  66. JOptionPane.showConfirmDialog(null, "当前版本只支持特定数据库","温馨提示",JOptionPane.YES_NO_OPTION);
  67. }
  68. } catch (Exception e1) {
  69. e1.printStackTrace();
  70. }
  71. }
  72. });
  73. no.addActionListener(new ActionListener() {
  74. @Override
  75. public void actionPerformed(ActionEvent e) {
  76. setVisible(false);
  77. }
  78. });
  79. JLabel label = new JLabel("数据库:");
  80. try {
  81. list = new DBService().getAllDatabases();
  82. } catch (Exception e1) {
  83. // TODO Auto-generated catch block
  84. e1.printStackTrace();
  85. }
  86. arrs = new String[list.size()];
  87. for(int i = 0; i < list.size(); i++){
  88. arrs[i] = list.get(i);
  89. }
  90. comboBox = new JComboBox(arrs);
  91. comboBox.setSelectedItem(dbName);
  92. comboBox.addItemListener(this);
  93. p1.add(scrollPanel);
  94. p2.setLayout(new FlowLayout(FlowLayout.LEFT));
  95. p2.add(label);
  96. p2.add(comboBox);
  97. p2.add(yes);
  98. p2.add(no);
  99. add("Center",p1);
  100. add("South",p2);
  101. setVisible(true);
  102. setSize(800,700);
  103. setLocation(100,100);
  104. }
  105. }
  106. public String readFromFile(String path){
  107. File file=new File(path);
  108. String s=null;
  109. try {
  110. FileInputStream fin=new FileInputStream(file);
  111. int length=fin.available();
  112. byte arr[]=new byte[length];
  113. int len=fin.read(arr);
  114. s=new String(arr,0,len);
  115. } catch (FileNotFoundException e) {
  116. e.printStackTrace();
  117. } catch (IOException e) {
  118. e.printStackTrace();
  119. }
  120. return s;
  121. }
  122. @Override
  123. public void itemStateChanged(ItemEvent e) {
  124. // TODO Auto-generated method stub
  125. if(e.getStateChange() == ItemEvent.SELECTED){
  126. dbName = "" + e.getItem();
  127. }
  128. }
  129. }

 

 

 

 

 

 

 

上面的都是界面类,然后现在贴出Service的代码

 


  
  1. package com.xmlDemo.service;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import com.xmlDemo.util.DBConnectionUtil;
  9. public class DBService {
  10. //获取某个数据库的所有数据表
  11. public List<String> getAllTables(String databaseName) throws Exception{
  12. List<String> list = new ArrayList<String>();
  13. int i = 0;
  14. String url = "jdbc:mysql://localhost:3306/"+databaseName;
  15. Connection connection = new DBConnectionUtil().getConnection(url);
  16. try {
  17. ResultSet rs=connection.getMetaData().getTables("","","",null);
  18. while (rs.next()) {
  19. list.add(rs.getString("TABLE_NAME"));
  20. }
  21. } catch (SQLException e) {
  22. e.printStackTrace();
  23. }
  24. return list;
  25. }
  26. public List<String> getAllDatabases() throws Exception{
  27. List<String> list = new ArrayList<String>();
  28. int i = 0;
  29. String sql = "show databases";
  30. String url="jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=UTF-8";
  31. Connection connection = new DBConnectionUtil().getConnection(url);
  32. try {
  33. PreparedStatement prepare = connection.prepareStatement(sql);
  34. ResultSet rs=prepare.executeQuery();
  35. while (rs.next()) {
  36. list.add(rs.getString(1));
  37. }
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. }finally{
  41. new DBConnectionUtil().close();
  42. }
  43. return list;
  44. }
  45. }

 

 

 

 

 

DBToXmlService.java:

 

 


  
  1. package com.xmlDemo.service;
  2. import java.io.FileOutputStream;
  3. import java.io.FileWriter;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Calendar;
  11. import java.util.Date;
  12. import org.dom4j.Document;
  13. import org.dom4j.DocumentHelper;
  14. import org.dom4j.Element;
  15. import org.dom4j.io.OutputFormat;
  16. import org.dom4j.io.XMLWriter;
  17. import com.xmlDemo.util.DBConnectionUtil;
  18. public class DBToXmlService {
  19. //导出xml文件
  20. public void exportDataToXMlFile(String dbName,String tableName) throws Exception {
  21. //创建文档并设置根元素userinfo
  22. Element root=DocumentHelper.createElement("userinfo");
  23. Document document=DocumentHelper.createDocument(root);
  24. //访问数据库并将数据库信息封装进创建的xml文档中
  25. accessDB(document, root,dbName,tableName);
  26. //指定文档输出格式
  27. OutputFormat format=new OutputFormat(" ", true);
  28. //定义输出流,输出文档,限于内存中,表现为在控制台输出
  29. XMLWriter xmlWriter=new XMLWriter(format);
  30. xmlWriter.write(document);
  31. //获取当前时间
  32. SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
  33. String time = sf.format(new Date());
  34. //把文档输出到存储设备,硬盘:第一种方式
  35. String fileName = dbName+"_"+tableName+"_"+time+".xml";
  36. XMLWriter xmlWriter2=new XMLWriter(new FileOutputStream("xml/"+fileName),format);
  37. xmlWriter2.write(document);
  38. //把文档输出到存储设备,硬盘:第二种方式
  39. XMLWriter xmlWriter3=new XMLWriter(new FileWriter("xml/"+fileName), format);
  40. xmlWriter3.write(document);
  41. //必须进行刷新和关闭,否则写入内容为空
  42. xmlWriter3.flush();
  43. }
  44. //定义静态函数访问数据库
  45. public static void accessDB(Document doc,Element root,String dbName,String tableName) {
  46. try {
  47. //数据库连接字符串
  48. String url="jdbc:mysql://localhost:3306/"+dbName;
  49. //连接数据库执行查询
  50. Connection connection=new DBConnectionUtil().getConnection(url);
  51. Statement statement=connection.createStatement();
  52. //获得数据库结果集
  53. ResultSet rs=statement.executeQuery("select * from "+tableName);
  54. //生成xml文档
  55. createXml(doc, root, rs);
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }finally{
  59. new DBConnectionUtil().close();
  60. }
  61. }
  62. //定义静态函数创建xml文档
  63. public static void createXml(Document doc,Element root,ResultSet rs) throws SQLException {
  64. while (rs.next()) {
  65. //生成与表名对应的元素节点并添加到根元素节点下
  66. Element user=root.addElement("users");
  67. //添加子元素userid
  68. Element userid=user.addElement("userid");
  69. userid.setText(rs.getString("userid"));
  70. //添加子元素username
  71. Element username=user.addElement("username");
  72. username.setText(rs.getString("username"));
  73. //添加子元素password
  74. Element password=user.addElement("password");
  75. password.setText(rs.getString("password"));
  76. }
  77. }
  78. }

 

 

 

 

 

 

XMLToDBService.java:

 


  
  1. package com.xmlDemo.service;
  2. import java.io.File;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.SQLException;
  6. import java.util.List;
  7. import org.dom4j.Document;
  8. import org.dom4j.DocumentException;
  9. import org.dom4j.Element;
  10. import org.dom4j.io.SAXReader;
  11. import com.xmlDemo.util.DBConnectionUtil;
  12. public class XmlToDBService {
  13. public void importDataIntoDB(String path,String dbName) throws Exception{
  14. //sql
  15. String sql="insert into users (userid,username,password) values(?,?,?)";
  16. //调用工具包里的数据库连接方法
  17. String url = "jdbc:mysql://localhost:3306/"+dbName+"?useUnicode=true&characterEncoding=UTF-8";
  18. Connection connection = new DBConnectionUtil().getConnection(url);
  19. //执行sql
  20. PreparedStatement presta=connection.prepareStatement(sql);
  21. //定义解析器
  22. SAXReader reader=new SAXReader();
  23. //获取文档对象
  24. Document document=reader.read(new File(path));
  25. //获取根元素
  26. Element root=document.getRootElement();
  27. //获取根元素下的用户集合
  28. List userList=root.elements();
  29. //双重循环遍历每一个用户下的子元素信息
  30. for (int i = 0; i < userList.size(); i++) {
  31. Element userElement=(Element)userList.get(i);
  32. List itemList=userElement.elements();
  33. System.out.println("第"+(i+1)+"个用户包含子元素个数:"+itemList.size());
  34. //遍历每个用户的子元素信息
  35. for (int j = 0; j< itemList.size(); j++) {
  36. Element element=(Element)itemList.get(j);
  37. //获取子元素信息进行参数设置
  38. presta.setString(j+1, element.getText());
  39. }
  40. //批量更新
  41. presta.addBatch();
  42. presta.executeBatch();
  43. }
  44. System.out.println("xml消息插入数据库成功!");
  45. new DBConnectionUtil().close();
  46. }
  47. }

 

 

 

 

 

 

然后是数据库连接的工具类:

 


  
  1. package com.xmlDemo.util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. /**
  8. * 数据库连接的工具类
  9. * @version 1.0.0
  10. */
  11. public class DBConnectionUtil {
  12. /**
  13. * 驱动
  14. */
  15. private String DRIVER="com.mysql.jdbc.Driver";
  16. /**
  17. * 链接
  18. */
  19. private String URL="jdbc:mysql://localhost:3306/xml?useUnicode=true&characterEncoding=UTF-8";
  20. /**
  21. * 用户名
  22. */
  23. private String USER="root";
  24. /**
  25. * 密码
  26. */
  27. private String PWD="111";
  28. Connection conn=null;
  29. PreparedStatement sta=null;
  30. ResultSet res=null;
  31. public DBConnectionUtil(){
  32. }
  33. /**
  34. * 连接数据库
  35. */
  36. public Connection getConnection(String url){
  37. try {
  38. Class.forName(DRIVER);
  39. conn=DriverManager.getConnection(url, USER, PWD);
  40. } catch (ClassNotFoundException e) {
  41. e.printStackTrace();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. return conn;
  46. }
  47. /**
  48. * 关闭数据库,释放内存
  49. */
  50. public void close(){
  51. try {
  52. if(res!=null){
  53. res.close();
  54. }
  55. if(sta!=null){
  56. sta.close();
  57. }
  58. if(conn!=null){
  59. conn.close();
  60. }
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. }

 

 

 

实现效果:

 

 

 

 

这是下载的链接:http://download.csdn.net/detail/u014427391/9357575

 

 

 



文章来源: smilenicky.blog.csdn.net,作者:smileNicky,版权归原作者所有,如需转载,请联系作者。

原文链接:smilenicky.blog.csdn.net/article/details/50315215

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。