воскресенье, 24 октября 2010 г.

MySQL & Cyrillic Symbols.

To work with Cyrillic symbols in MySQL:
    sudo gedit /etc/mysql/my.cnf
Add next settings in section [mysqld]:
    default-character-set=utf8
character-set-server=utf8
collation-server=utf8_general_ci
init-connect="SET NAMES utf8"
skip-character-set-client-handshake
and this to section: [mysqldump]:
    default-character-set=utf8
reboot mysql and enjoy!
Also create table code:
    CREATE TABLE $tablename CHARACTER SET utf8

суббота, 23 октября 2010 г.

Java Samples 4. jFrame close confirmation.

...
main.this.addWindowListener(new WindowListener() {
public void windowClosing(WindowEvent event) {
Object[] options = { "Yes", "No" };
int n = JOptionPane.showOptionDialog(event.getWindow(), "Close Window?",
"Close Window?", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[0]);
if (n == 0) event.getWindow().setVisible(false);
}
public void windowActivated(WindowEvent event) {}
public void windowClosed(WindowEvent event) {}
public void windowDeactivated(WindowEvent event) {}
public void windowDeiconified(WindowEvent event) {}
public void windowIconified(WindowEvent event) {}
public void windowOpened(WindowEvent event) {}
});
initComponents();
...
Also change defaultCloseOperation from EXIT_ON_CLOSE to DO_NOTHING_ON_CLOSE

суббота, 16 октября 2010 г.

суббота, 9 октября 2010 г.

Java Samples 2. SQL connections.

public static void voidName(String query) throws SQLException {
Connection connectionName = DriverManager.getConnection("url" /* хост:порт/база */, "user", "pass");
if (connectionName==null) {
System.exit(0);
}
Statement statementName = connectionName.createStatement();
ResultSet resultsetName = statementName.executeQuery("query");
while(resultsetName.next()) {
resultsetName.getString("columnName");
some_actions_with_columnName
}
}

воскресенье, 3 октября 2010 г.

Java Samples 1 (String & File)

/* Load file content to string */

public static String fileView(String filename) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
StringBuilder s = new StringBuilder("");
while (br.ready()) s.append(br.readLine()+"\n");
return(s.toString());
}


/* Write text to file*/

public static void writeToFile(String fileName, String text) throws IOException {
try {
File file = new File(fileName);
FileWriter fw = new FileWriter(file, true);
fw.append(text);
fw.close();
}
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}

Java Samples 0 (String & ArrayList)

/* delete spaces around the string */

public static String deleteSpaces(String x) {
if (x.startsWith(" ")) x = x.substring(1);
if (x.endsWith(" ")) x = x.substring(0, x.length()-1);
return(x);
}


/* convert ArrayList to String */
public static String listToString(ArrayList list) {
String s = null;
if (!list.isEmpty) {
for (int i = 0; i < list.size(); i++) {
s.concat(list.get(i).toString()+"\n");
}
}
return(s);
}


/* convert String to ArrayList */
public static ArrayList stringToArrayList(String string) {
String[] pieces = string.split("\n");
for (int i=pieces.length-1;i>=0;i--) {
pieces[i] = pieces[i].trim();
}
ArrayList a = new ArrayList(Arrays.asList(pieces));
return (a);
}