'IT일반/Java'에 해당되는 글 4건
- 2008.11.20 String 자르기!
- 2008.11.14 email 체크 정규식*Java 1
- 2008.08.12 sun.net.smtp.SmtpClient을 이용한 메일 보내기
- 2008.07.14 sybase 간단한 접속 테스트
2008. 11. 20. 10:59
String 자르기!
2008. 11. 20. 10:59 in IT일반/Java
public class CutTest {
public static void main(String[] args) {
String str = "あいおうおあいおうおあいおうお";
String str1 = "abcdeabcdeabcdeabcde";
System.out.println(12 + ": [" + cutStringByBytes(str, 9) + "]");
System.out.println(12 + ": [" + cutStringByBytes(str1, 9) + "]");
}
public static String spaces(int count) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < count; i++) {
sb.append(" ");
}
return sb.toString();
}
public static String cutInStringByBytes(String str, int length) {
byte[] bytes = str.getBytes();
int len = bytes.length;
int counter = 0;
if (length >= len) {
return str + spaces(length - len);
}
for (int i = length - 1; i >= 0; i--) {
if (((int)bytes[i] & 0x80) != 0)
counter++;
}
return new String(bytes, 0, length - (counter % 2));
}
public static String cutStringByBytes(String str, int length) {
byte[] bytes = str.getBytes();
int len = bytes.length;
int counter = 0;
if (length >= len) {
return str + spaces(length - len);
}
for (int i = length - 1; i >= 0; i--) {
if (((int)bytes[i] & 0x80) != 0)
counter++;
}
return new String(bytes, 0, length + (counter % 2));
}
}
2008. 11. 14. 15:19
email 체크 정규식*Java
2008. 11. 14. 15:19 in IT일반/Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public boolean isValidEmail() {
boolean err = false;
String regex = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@(?:\\w+\\.)+\\w+$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(inputStr);
if( !m.matches() ) {
String[] params = new String[] { item };
DialogManager.showMessage(comp, DialogManager.ERROR_INPUT_INVALID_EMAIL, params);
err = true;
}
return err;
}
2008. 8. 12. 15:28
sun.net.smtp.SmtpClient을 이용한 메일 보내기
2008. 8. 12. 15:28 in IT일반/Java
package jp.co.ntl.application.printauth;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import sun.net.smtp.SmtpClient;
public class FreeSendMailer extends JFrame
implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextArea message;
private JTextField server;
private JTextField to;
private JTextField from;
private JTextField title;
private JButton btn;
private String serverName = "localhost";
private String fromName = "from@xxx.xx";
private String toName = "to@xxx.xx.xx";
public FreeSendMailer() {
this.setSize(300, 400);
this.setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 1));
server = new JTextField(serverName);
from = new JTextField(fromName);
to = new JTextField(toName);
title = new JTextField("Mail Title");
JPanel p1 = new JPanel();
JLabel l1 = new JLabel("SERVER:");
l1.setPreferredSize(new Dimension(80,20));
p1.add(l1);
p1.add(server);
JPanel p2 = new JPanel();
JLabel l2 = new JLabel("FROM:");
l2.setPreferredSize(new Dimension(80,20));
p2.add(l2);
p2.add(from);
JPanel p3 = new JPanel();
JLabel l3 = new JLabel("TO:");
l3.setPreferredSize(new Dimension(80,20));
p3.add(l3);
p3.add(to);
JPanel p4 = new JPanel();
JLabel l4 = new JLabel("TITLE:");
l4.setPreferredSize(new Dimension(80,20));
p4.add(l4);
p4.add(title);
server.setPreferredSize(new Dimension(200,20));
from.setPreferredSize(new Dimension(200,20));
to.setPreferredSize(new Dimension(200,20));
title.setPreferredSize(new Dimension(200,20));
p.add(p1);
p.add(p2);
p.add(p3);
p.add(p4);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(p, BorderLayout.NORTH);
message = new JTextArea();
message.setLineWrap(true);
JScrollPane scroll = new JScrollPane();
scroll.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.getViewport().add(message);
getContentPane().add(scroll, BorderLayout.CENTER);
btn = new JButton("send");
btn.addActionListener(this);
getContentPane().add(btn, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new FreeSendMailer().setVisible(true);
}
public void actionPerformed(ActionEvent ev) {
String to1 = to.getText();
String from1 = from.getText();
String server1 = server.getText();
String title1 = title.getText();
String msg = message.getText();
try {
title1 = new String(title1.getBytes("ISO2022JP"));
msg = new String(msg.getBytes("ISO2022JP"));
SmtpClient client = new SmtpClient(server1);
client.from(from1);
client.to(to1);
PrintStream stream = client.startMessage();
stream.println("To:" + to1);
stream.println("Subject:" + title1);
stream.println("Content-Type: text/plain; charset=iso-2022jp");
stream.println(msg);
stream.flush();
stream.close();
JOptionPane.showMessageDialog(this, "success!");
} catch(Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "failed\n"
+ ex.toString());
}
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import sun.net.smtp.SmtpClient;
public class FreeSendMailer extends JFrame
implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextArea message;
private JTextField server;
private JTextField to;
private JTextField from;
private JTextField title;
private JButton btn;
private String serverName = "localhost";
private String fromName = "from@xxx.xx";
private String toName = "to@xxx.xx.xx";
public FreeSendMailer() {
this.setSize(300, 400);
this.setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 1));
server = new JTextField(serverName);
from = new JTextField(fromName);
to = new JTextField(toName);
title = new JTextField("Mail Title");
JPanel p1 = new JPanel();
JLabel l1 = new JLabel("SERVER:");
l1.setPreferredSize(new Dimension(80,20));
p1.add(l1);
p1.add(server);
JPanel p2 = new JPanel();
JLabel l2 = new JLabel("FROM:");
l2.setPreferredSize(new Dimension(80,20));
p2.add(l2);
p2.add(from);
JPanel p3 = new JPanel();
JLabel l3 = new JLabel("TO:");
l3.setPreferredSize(new Dimension(80,20));
p3.add(l3);
p3.add(to);
JPanel p4 = new JPanel();
JLabel l4 = new JLabel("TITLE:");
l4.setPreferredSize(new Dimension(80,20));
p4.add(l4);
p4.add(title);
server.setPreferredSize(new Dimension(200,20));
from.setPreferredSize(new Dimension(200,20));
to.setPreferredSize(new Dimension(200,20));
title.setPreferredSize(new Dimension(200,20));
p.add(p1);
p.add(p2);
p.add(p3);
p.add(p4);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(p, BorderLayout.NORTH);
message = new JTextArea();
message.setLineWrap(true);
JScrollPane scroll = new JScrollPane();
scroll.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.getViewport().add(message);
getContentPane().add(scroll, BorderLayout.CENTER);
btn = new JButton("send");
btn.addActionListener(this);
getContentPane().add(btn, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new FreeSendMailer().setVisible(true);
}
public void actionPerformed(ActionEvent ev) {
String to1 = to.getText();
String from1 = from.getText();
String server1 = server.getText();
String title1 = title.getText();
String msg = message.getText();
try {
title1 = new String(title1.getBytes("ISO2022JP"));
msg = new String(msg.getBytes("ISO2022JP"));
SmtpClient client = new SmtpClient(server1);
client.from(from1);
client.to(to1);
PrintStream stream = client.startMessage();
stream.println("To:" + to1);
stream.println("Subject:" + title1);
stream.println("Content-Type: text/plain; charset=iso-2022jp");
stream.println(msg);
stream.flush();
stream.close();
JOptionPane.showMessageDialog(this, "success!");
} catch(Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "failed\n"
+ ex.toString());
}
}
}
2008. 7. 14. 18:23
sybase 간단한 접속 테스트
2008. 7. 14. 18:23 in IT일반/Java
import java.sql.*;
public class SybaseDBConn {
public static void main( String[ ] args ) {
try {
Class.forName( "com.sybase.jdbc2.jdbc.SybDriver" );
} catch( ClassNotFoundException ex ) {
System.err.printf( "error : ", ex.toString() );
System.exit( 0 );
}
Connection conn = null;
String url = "jdbc:sybase:Tds:127.0.0.1:2638/SPSDB";
String id = "DBA";
String pass = "sql";
Statement stmt = null;
ResultSet rs = null;
String query = "SELECT * FROM sps_systemprefstable";
try {
conn = DriverManager.getConnection( url, id, pass );
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
while( rs.next() ) {
System.out.println( rs.getInt(1) + " : " + rs.getString(2) );
}
rs.close( );
stmt.close( );
conn.close( );
} catch( SQLException ex ) {
System.err.println( "SQL Exception : " + ex.toString() );
System.exit( 0 );
}
}
}
public class SybaseDBConn {
public static void main( String[ ] args ) {
try {
Class.forName( "com.sybase.jdbc2.jdbc.SybDriver" );
} catch( ClassNotFoundException ex ) {
System.err.printf( "error : ", ex.toString() );
System.exit( 0 );
}
Connection conn = null;
String url = "jdbc:sybase:Tds:127.0.0.1:2638/SPSDB";
String id = "DBA";
String pass = "sql";
Statement stmt = null;
ResultSet rs = null;
String query = "SELECT * FROM sps_systemprefstable";
try {
conn = DriverManager.getConnection( url, id, pass );
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
while( rs.next() ) {
System.out.println( rs.getInt(1) + " : " + rs.getString(2) );
}
rs.close( );
stmt.close( );
conn.close( );
} catch( SQLException ex ) {
System.err.println( "SQL Exception : " + ex.toString() );
System.exit( 0 );
}
}
}