标签:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import java.util.ArrayList; import java.util.Iterator; import com.perssoft.common.tool.EncryptUtil; import com.perssoft.common.tool.PinYinUtil; import com.perssoft.plugin.activerecord.Db; import com.perssoft.plugin.activerecord.Record; public class ImportData { /** * @param args */ public static void main(String[] args) { try { String filepath="C://机构表//测试.xls"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:driver={Microsoft Excel Driver (*.xls)};DBQ="+filepath,"",""); String strq = "select * from [Sheet1$] where 1=1 "; PreparedStatement ps = con.prepareStatement(strq); ResultSet rs = ps.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); ArrayList<String> templist=new ArrayList<String>(); int i=rsmd.getColumnCount(); for (int j = 1; j < i+1; j++) { templist.add(rsmd.getColumnName(j)); } String deptname=""; int ordernum=1; while (rs.next()) { String unitname = rs.getString(templist.get(0)); String chuji = rs.getString(templist.get(1)); String keji = rs.getString(templist.get(2)); String name = rs.getString(templist.get(3)); String remark = rs.getString(templist.get(4)); String deptid = rs.getString(templist.get(5)); System.out.println(unitname+" "+chuji+" "+keji+" "+name+" "+remark+" "+deptid); //保存部门和个人信息 //查询是否存在该人 Connection conn = getConnection(); Statement stmt = conn.createStatement(); ResultSet rs1 = stmt.executeQuery("select * from xt_admin where name =‘"+name.toString()+"‘ and isdelete=0 "); if(rs1.next()){ System.out.println("存在重名者:"+name +" ID:"+rs1.getBigDecimal(1)); stmt.close(); rs1.close(); conn.close(); continue; }else{//不存在 String sql="insert into XX(ID,LOGINNAME,NAME,PASSWORD,STATE)values(S_ADMIN.nextval)"; System.out.println(sql); Statement statament =conn.createStatement(); statament.executeUpdate(sql); statament.close(); } stmt.close(); rs1.close(); conn.close(); } con.close(); } catch (Exception e) { e.printStackTrace(); } } public static Integer save(String sql) throws Exception{ // 采用Statement进行查询 Connection conn = getConnection(); PreparedStatement pstmt=null; pstmt=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); pstmt.executeUpdate(); ResultSet rs=pstmt.getGeneratedKeys(); Integer key=null; if (rs.next()) { key= rs.getInt(1); } rs.close(); pstmt.close(); conn.close(); return key; } public static Connection getConnection() throws Exception { // 定义了数据库连接串 String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; // 数据库的用户名 String user = "oa"; // 数据库的用户口令 String password = "123"; // 加载jdbc-odbc bridge驱动程序 Class.forName("oracle.jdbc.driver.OracleDriver"); // 与url指定的数据源建立连接 Connection c = DriverManager.getConnection(dbUrl, user, password); return c; } }
标签:
原文地址:http://www.cnblogs.com/sunyt/p/4809374.html