Professional Documents
Culture Documents
One Day Java Trip
One Day Java Trip
sd :
1433 2012
.
.
.NetBeans .
.
.
.
Byte code .
.
Java Virtual Machine JVM .Java Run-time
.
32 32
.
64 64 . 64:
jdk6u16windowsx64.exe
1.6 6
7 32:
openjdk7jre
.
.
.
.
NetBeans
.
PHP .++
Eclipse NetBeans
.
:
. :
.1 .
.
.2 .
.3 .
.4
creative commons
2............................................................................................................................................
2..........................................................................................................................................
3....................................................................................................................... NetBeans
: 3......................................................................................................................
3..................................................................................................................................
5................................................................................................................................. .
10.......................................................................................................................
14..................................................................................................................................
16...........................................................................................................................
21....................................................................................................................
23.........................................................................................................................
25......................................................................................................... New Class
30.................................................................................................................. SQLite
31................................................................................................. SQLite
40.................................................................................................................
.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hello;
/*
*
* @author motaz
*/
public class Hello {
/*
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}
NetBeans hello.java
:
main
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hello;
/**
*
* @author motaz
*/
public class Hello {
/**
* @param args the command line arguments
*/
{ )public static void main(String[] args
// TODO code application logic here
;)"System.out.print("Hello Java world\n
}
}
F6 NetBeans
Build
. Shift + F11 NetBeans
NetBeansProjects hello dist .
hello.jar
:
java -jar hello.jar
Byte code
. ) (
.
:
int num = 9;
System.out.print(num + " * 2 = " + num * 2 + "\n");
9 num
.2
.\ n
:
Date today = new Date();
System.out.print("Today is: " + today.toString() + "\n");
: Date
import java.util.Date;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hello;
import java.util.Date;
/*
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int num = 9;
}
}
:
Class
:
NetBeans .
.Widgets
GTK QT
Carbon .Cocoa
.Swing
NetBeans File/New Project
Java/Java Application
.mygui
Projects mygui New/JFrame Form
MainForm :
MainForm.java .
.Palette Button
Hello F2
Properties Text
. Events
actionPerformed jButton1ActionPerformed :Source
double click
) ( . :
{privatevoidjButton1ActionPerformed(java.awt.event.ActionEventevt)
;" "Stringmsg=
;)JOptionPane.showMessageDialog(null,msg
}
:
msg String
Mygui.java :main
Design TextField
Label )( Label jlName
Code Variable Name
)( :
ActionPerfomed jLabel2
:
Java /Java Desktop Application
7.2
NetBeans
. New/Java/Java Application
:
defaultCloseOperation Dispose
EXIT_ON_CLOSE .
.
.
MainForm ActionPerformed
.
;)(SecondForm second = new SecondForm
;)second.setVisible(true
.
.
constructor SecondForm
:
public SecondForm(String atext) {
initComponents();
jLabel1.setText(atext);
}
jLabel1
:
) (console application
.Java/Java Application
files main .
writeToTextFile :
aFileName
text .
;)textWriter.writeBytes(text
;)(textWriter.close
;)(fstream.close
return (true); // success
}
)catch (Exception e
{
;))(System.err.println("Error: " + e.getMessage
return (false); // fail
}
exception handling
.
:
{try
// Put the code you want to protect here
;)return (true
}
)catch (Exception e
{
;))(System.err.println("Error: " + e.getMessage
return (false); // fail
}
try
catch e .Exception
true .
false .
fstream FileOutputStream .
.
textWriter DataOutputStream
.fstream
textWriter :
;)textWriter.writeBytes(text
main :
;)"writeToTextFile("myfile.txt", "my text
.
:
.false result
if (result)
true result
else
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
: readTextFile
byte ch;
while (textReader.available() != 0) {
ch = textReader.readByte();
System.out.write(ch);
}
textReader.close();
fstream.close();
return (true); // success
}
catch (Exception e)
{
System.err.println("Error in readTextFile: " + e.getMessage());
return (false); // fail
}
while . Output
Input
available .
textReader
textReader.available()
:
while (textReader.available() != 0)
.
byte ch
. character
BufferedReader lineReader =
new BufferedReader(new InputStreamReader (textReader));
String line;
while ((line = lineReader.readLine()) != null)
System.out.println (line);
fstream.close();
return (true); // success
}
catch (Exception e)
{
System.err.println("Error in readTextFile: " + e.getMessage());
return (false); // fail
}
.BufferedReader lineReader
:
.InputStreamReader
BufferedReader lineReader =
new BufferedReader(new InputStreamReader (textReader));
lineReader isr
. isr
:
.main
readTextFile("/home/motaz/java.txt");
}
.
:
;Date today
;)(today = new Date
today .Date
today .
new
;)(Date
.today
.constructor
;today = null
today .
garbage collector
.
.
:
;Date today
;Date today2
;)(today = new Date
;today2 = today
;today = null
;)"System.out.print("Today is: " + today2.toString() + "\n
today
today2
.
today .today2
garage collector .
today2
.
try catch :
{ try
;Date today
;Date today2
;)(today = new Date
;today2 = today
;today = null
;today2 = null
;)"System.out.print("Today is: " + today2.toString() + "\n
{ )} catch (Exception e
;)"System.out.print("Error: " + e.toString() + "\n
}
java.lang.NullPointerException
main Openfile.java
:
{ )public static void main(String[] args
readTextFile
.Console
JTextArea
:
String line;
textArea.setText("");
fstream.close();
return (true); // success
}
catch (Exception e)
{
textArea.append("Error in readTextFile: " + e.getMessage() + "\n");
: ActionPerformed
:
final JFileChooser fc = new JFileChooser ();
: .
:
int result = fc.showOpenDialog(null);
: jLabel1
if (result == JFileChooser.APPROVE_OPTION) {
jLabel1.setText(fc.getSelectedFile().toString());
readTextFile(fc.getSelectedFile().toString(), jTextArea1);
}
:
New Class
Label Text Area .JFrameForm
classes .
class .
MainForm JFrameForm
*/
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
;package newclass
*/
*
* @author motaz
*/
{ public class Sentence
- }{ - mySentence
.
;String mySentence
atext mySentence .
atext Sentence .
.
getFirst :
{)(public String getFirst
;String first
;int firstSpaceIndex
;)" "(firstSpaceIndex = mySentence.indexOf
)if (firstSpaceIndex == -1
;first = mySentence
else
;)first = mySentence.substring(0, firstSpaceIndex
;)return (first
}
indexOf mySentence
.
.
indexOf 1-
) = (.
substring
. first
Sentence getLast :
)if (lastSpaceIndex == -1
;last = mySentence
else
;))(last = mySentence.substring(lastSpaceIndex + 1, mySentence.length
;)return (last
) (lastIndexOf
mySentence.length
:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newclass;
/*
*
* @author motaz
*/
public class Sentence {
String mySentence;
super();
mySentence = atext;
}
String first;
int firstSpaceIndex;
firstSpaceIndex = mySentence.indexOf(" ");
if (firstSpaceIndex == -1)
first = mySentence;
else
first = mySentence.substring(0, firstSpaceIndex);
return (first);
}
String last;
int lastSpaceIndex;
lastSpaceIndex = mySentence.lastIndexOf(" ");
if (lastSpaceIndex == -1)
last = mySentence;
else
last = mySentence.substring(lastSpaceIndex + 1, mySentence.length());
return (last);
}
}
MainForm.java
: .Text Field
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
:
mySent = new Sentence(jTextField1.getText());
getText jTextField1
.
SQLite
SQLite self-contained
.SQLite SQL .
BlackBerry
SQLite :
http://sqlite.org/download.html
sqlite-shell
. sqlite3
terminal
:
sqlite3 library.db
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
";" Enter SQL statements terminated with a
>sqlite
library.db
books :
.
;))sqlite> create table books(BookId int, BookName varchar(100
:
;)"sqlite> insert into books values (1, "Introduction to Java 7
;)"sqlite> insert into books values (2, "One day trip with Java
:
;sqlite> select * from books
1|Introduction to Java 7
2|One day trip with Java
>sqlite
library.db .books
.
SQLite
SQLite .
. :
http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC
sqlite-jdbc . :
sqlitejdbc3.7.2.jar
:
http://www.xerial.org/maven/repository/artifact/org/xerial/sqlite-jdbc/3.7.2/
SQLite
.
.
sqlitebrowser Library
constructor :
// Constructor
{ )public SqliteClient (String aDatabaseName
;)(super
{ try
;)"Class.forName("org.sqlite.JDBC
(dbConnection = DriverManager .getConnection
;)"jdbc:sqlite:" + aDatabaseName
}
{)catch (Exception e
;))(System.out.println("Error while connecting: " + e.toString
}
try .. catch
SQLite .
) (Class.forName SQLite
.
dbConnection
SqliteClient
showTable SqliteClient
:
{ try
;)(myQuery = dbConnection.createStatement
;)myRecords = myQuery.executeQuery("SELECT * from " + aTable
// Read records
))(while (myRecords.next
{
" textArea.append(myRecords.getString(1) + " -
;)"+ myRecords.getString(2) + "\n
}
{)catch (Exception e
Statement java.sql.Statement
java.beans.Statement .
:
:
;)(myQuery = dbConnection.createStatement
executeQuery myQuery SQL
. .ResultSet myRecords
.
ResultSet
:showTable
// Read records
))(while (myRecords.next
{
" textArea.append(myRecords.getString(1) + " -
;)"+ myRecords.getString(2) + "\n
}
next
.true
false
.
getString
Field/Column
.
Open MainForm books
library.db
{ )private void jButton1ActionPerformed(java.awt.event.ActionEvent evt
;)""(jTextArea1.setText
;)sql.showTable("books", jTextArea1
}
sql SqliteClient
. .
. showTable
books :
insertBook
tables
SqliteClient .showTable :
{ try
(PreparedStatement insertRecord = dbConnection.prepareStatement
;)")? "insert into books (BookID, BookName) values (?,
;)insertRecord.setInt(1, bookID
;)insertRecord.setString(2, bookName
;)(insertRecord.execute
;)return(true
}
{)catch (Exception e
;))(System.out.println("Error while reading table: " + e.toString
;)return (false
}
insertRecord PreparedStatement
DML .
.values .parameters .
setInt
setString execute
SQLite library.db
Exit_On_Close
defaultCloseOperation Dispose
Insert :
{ )private void jButton1ActionPerformed(java.awt.event.ActionEvent evt
query SqliteClient
bookID .
- - trim String
parseInt
. trim
jTextField1 . :
.
;)(myText = aText.trim
insertBook query
. setVisible .false
:
build shift + F11 lib dist
.
lib .
sqlitejdbc3.7.2.jar .lib
library.db .
jTexField :
:
;))(SqliteClient sql = new SqliteClient(jTextField1.getText
portable .
.
timer MainForm JFrame Form new class
MyTimer :
inheritance
. .TimerTask :
myLabel :
;JLabel myLabel
alabel .myLabel
run :
@Override
{ )(public void run
;)(Date today = new Date
;))(myLabel.setText(today.toString
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package timer;
import java.util.Date;
import java.util.TimerTask;
import javax.swing.JLabel;
/*
*
* @author motaz
*/
public class MyTimer extends TimerTask{
JLabel myLabel;
public MyTimer(JLabel alabel){
super();
myLabel = alabel;
}
@Override
public void run() {
Date today = new Date();
myLabel.setText(today.toString());
: MainForm jLabel
:
{ )(public MainForm
;)(initComponents
;java.util.Timer generalTimer = null
:
;java.util.Timer generalTimer = null
generalTimer .Timer .
timerObj MyTimer .
1000 . .
:
.
.
code.sd