csv导入数据库 java源代码

  • s9_697468
    了解作者
  • 8.6KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-20 03:19
    上传日期
csv导入数据库 java源代码
ScanCSVToDB.rar
  • ScanCSVToDB
  • src
  • client
  • ScanCSVTest.java
    422B
  • business
  • ScanCSVToDB.java
    2KB
  • dao
  • StudentDAO.java
    2.1KB
  • entity
  • Student.java
    959B
  • .settings
  • org.eclipse.jdt.core.prefs
    629B
  • bin
  • client
  • ScanCSVTest.class
    770B
  • business
  • ScanCSVToDB.class
    2.4KB
  • dao
  • StudentDAO.class
    3.1KB
  • entity
  • Student.class
    874B
  • .project
    387B
  • Student.csv
    48B
  • .classpath
    393B
内容介绍
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.List; import entity.Student; /** * 操作数据库中student表的dao类 * @author 刘善廷 * */ public class StudentDAO { private String driver = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://localhost/student"; private String userName = "root"; private String password = "ting"; private Connection conn = null; private Statement stmt = null; /** * 加载驱动 * @throws ClassNotFoundException * 类未找到异常 */ private void loadDriver() throws ClassNotFoundException { Class.forName(driver); } /** * 取得数据库连接 * @throws SQLException * SQL异常 * @throws ClassNotFoundException * 类未找到异常 */ private void getConnection() throws SQLException, ClassNotFoundException { loadDriver(); conn = DriverManager.getConnection(url, userName, password); } /** * 关闭数据库连接资源 */ private void close() { try { if (conn != null) { conn.close(); } if (stmt != null) { stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } /** * 数据库插入 * @param studentList * 含有student类型的实例的列表 * @return * 返回int[]的插入结果 */ public int[] insertStudents(List<Student> studentList) { int[] results=null; StringBuffer sb=new StringBuffer(); try { getConnection(); /** * 利用stmt的存储过程 */ stmt=conn.createStatement(); stmt.addBatch("delete from student"); for (Student student : studentList) { sb.setLength(0); sb.append("insert into student values("); sb.append(student.getId()+","+"'"+student.getName()+"','"+student.getGender()+"'"); sb.append(")"); stmt.addBatch(sb.toString()); } /** * 执行stmt内存储的sql语句 */ results= stmt.executeBatch(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally{ close(); } return results; } }
评论
    相关推荐