boolean sessionDebug = false;
InternetAddress[] address;
//設定所要用的Mail伺服器和所使用的傳輸協定
java.util.Properties props = System.getProperties();
props.put("mail.host","202.203.201.11");
props.put("mail.transport.protocol","smtp");
//產生新的maill session
javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props,null);
mailSession.setDebug(sessionDebug);
MimeMessage msg = new MimeMessage(mailSession);
//設定傳送郵件的發信人
msg.setFrom(new InternetAddress("ccccxxx@gmail.com","USERNAME"));
//設定收信人的信箱
address = InternetAddress.parse(sendmail,false);
msg.setRecipients(Message.RecipientType.TO, address);
//設定郵件主題
msg.setSubject(sub,"BIG5");
//設定發信時間
msg.setSentDate(new java.util.Date());
//設定郵件內容
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
//設定傳送信的MIME Type
mbp.setContent(message,"text/html;charset=Big5");
mp.addBodyPart(mbp);
msg.setContent(mp);
//傳送郵件
Transport.send(msg);
2015年9月10日 星期四
Java : 讀取目錄下所有CSV檔案內資料 (get all CSV date in same folder)
public class csv {
public static void main(String[] args)
{
FileReader fr = null;
File dir = new File("F:/log");
File[] files = dir.listFiles();
try
{
if(files != null)
{
for (int i = 0; i < files.length; i++)
{
String filePath = files[i].getAbsolutePath().toLowerCase().trim();
String filename=files[i].getName();
int startIndex = filename.lastIndexOf('.') + 1;
String fcsv = filename.substring(startIndex);
System.out.println( "fcsv :"+fcsv );
if(fcsv.equals("csv"))
{
System.out.println(filePath);
System.out.println( "filesName():"+filename );
fr = new FileReader(filePath);
//放到BufferedReader一筆筆讀出
BufferedReader br = new BufferedReader(fr);
ArrayList taglist = new ArrayList(getTagList(br));
for(int k=0; k<taglist.size(); k++)
{
System.out.println( k+":" );
System.out.println( ((String[])taglist.get(k))[0]);
System.out.println( ((String[])taglist.get(k))[1]);
System.out.println( ((String[])taglist.get(k))[2]);
System.out.println( ((String[])taglist.get(k))[3]);
System.out.println( "--------" );
}
br.close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static ArrayList getTagList (BufferedReader br)
{
ArrayList taglist = new ArrayList();
String sData = null;
try
{
while ((sData = br.readLine()) != null)
{
String strArray[] = sData.split(",");
taglist.add(strArray);
}
}
catch (Exception e){
e.printStackTrace();
}
return taglist;
}
}
public static void main(String[] args)
{
FileReader fr = null;
File dir = new File("F:/log");
File[] files = dir.listFiles();
try
{
if(files != null)
{
for (int i = 0; i < files.length; i++)
{
String filePath = files[i].getAbsolutePath().toLowerCase().trim();
String filename=files[i].getName();
int startIndex = filename.lastIndexOf('.') + 1;
String fcsv = filename.substring(startIndex);
System.out.println( "fcsv :"+fcsv );
if(fcsv.equals("csv"))
{
System.out.println(filePath);
System.out.println( "filesName():"+filename );
fr = new FileReader(filePath);
//放到BufferedReader一筆筆讀出
BufferedReader br = new BufferedReader(fr);
ArrayList taglist = new ArrayList(getTagList(br));
for(int k=0; k<taglist.size(); k++)
{
System.out.println( k+":" );
System.out.println( ((String[])taglist.get(k))[0]);
System.out.println( ((String[])taglist.get(k))[1]);
System.out.println( ((String[])taglist.get(k))[2]);
System.out.println( ((String[])taglist.get(k))[3]);
System.out.println( "--------" );
}
br.close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static ArrayList getTagList (BufferedReader br)
{
ArrayList taglist = new ArrayList();
String sData = null;
try
{
while ((sData = br.readLine()) != null)
{
String strArray[] = sData.split(",");
taglist.add(strArray);
}
}
catch (Exception e){
e.printStackTrace();
}
return taglist;
}
}
Java : 連結DB2資料庫(connect to DB2 database)
public class connectDb2{
public static void main(String[] args) throws Exception
{
DB_URL = "jdbc:db2://10.10.10.18/XXX";
Class.forName("com.ibm.db2.jcc.DB2Driver");
conn = DriverManager.getConnection(DB_URL, "USER", "PW");
//select
PreparedStatement preStmt = conn.prepareStatement("Select * from DB2DATA");
ResultSet rs = preStmt.executeQuery();
if(rs.next())
{
String no = rs.getString("NO").toString();
}
//insert
PreparedStatement preStmt = conn.prepareStatement("insert into DB2DATA");
preStmt.execute();
}
}
public static void main(String[] args) throws Exception
{
DB_URL = "jdbc:db2://10.10.10.18/XXX";
Class.forName("com.ibm.db2.jcc.DB2Driver");
conn = DriverManager.getConnection(DB_URL, "USER", "PW");
//select
PreparedStatement preStmt = conn.prepareStatement("Select * from DB2DATA");
ResultSet rs = preStmt.executeQuery();
if(rs.next())
{
String no = rs.getString("NO").toString();
}
//insert
PreparedStatement preStmt = conn.prepareStatement("insert into DB2DATA");
preStmt.execute();
}
}
Java : 連結access資料庫(connect to MS access database)
public class connectAccess{
public static void main(String[] args) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//access 路徑
String str1="jdbc:odbc:DRIVER=Microsoft Access Driver(*.mdb);DBQ=C:/acc.mdb";
Connection conn=DriverManager.getConnection(str1,"","");
Statement stmt = conn.createStatement();
ResultSet rs;
try
{
//select
rs = stmt.executeQuery("select * from data" );
while(rs.next())
{
String no=rs.getString("NO").toString();
}
//insert
stmt.executeUpdate("insert into data");
//update
stmt.executeUpdate("update data set NO=''");
} catch (Exception e) {
e.printStackTrace();
}finally{
stmt.close();
conn.close();
}
}
}
public static void main(String[] args) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//access 路徑
String str1="jdbc:odbc:DRIVER=Microsoft Access Driver(*.mdb);DBQ=C:/acc.mdb";
Connection conn=DriverManager.getConnection(str1,"","");
Statement stmt = conn.createStatement();
ResultSet rs;
try
{
//select
rs = stmt.executeQuery("select * from data" );
while(rs.next())
{
String no=rs.getString("NO").toString();
}
//insert
stmt.executeUpdate("insert into data");
//update
stmt.executeUpdate("update data set NO=''");
} catch (Exception e) {
e.printStackTrace();
}finally{
stmt.close();
conn.close();
}
}
}
訂閱:
文章 (Atom)