localhost
is the machine where you are working. The port
number of most web servers is 80.telnet java.sun.com 80
GET /index.html HTTP/1.0
(plus blank line; press Enter
twice)java.net
.Socket s = new Socket("java.sun.com", 80);
InputStream in = s.getInputStream(); OutputStream out =
s.getOutputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(in));
PrintWriter writer = new PrintWriter(out);
writer.print("GET /index.html HTTP/1.0\n\n");
writer.flush();
String line; while ((line = reader.readLine()) != null)
System.out.println(line);
s.close();
ServerSocket server = new ServerSocket(5000);
Socket s = server.accept();
DatagramSocket socket = new DatagramSocket(port);
byte[] data = new byte[256];
// put data into this arrayDatagramPacket sendPacket = new DatagramPacket (data, data.length,
address, port);
socket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket (data,
data.length);
socket.receive(receivePacket);
String message = new String(receivePacket.getData());
URL u = new URL("http://home.umu.ac.ug/index.html");
URLConnection c = u.openConnection();
InputStream in = c.getInputStream();
getProtocol()
,
getHost()
, getPort()
and getFile()
.getResponseMessage()
can return 404 (not found), 200 (OK) or
other status codes.GET www.weather.com/cgi-bin/weatherprogram?city=Kampala&country=Uganda
CREATE TABLE tablename (
variablename variabletype, variablename variabletype ... )
CREATE TABLE Products ( Product_Code CHAR(11), Description
CHAR(40), Price DECIMAL(10, 2) )
INSERT INTO Products VALUES ('3354', 'Sony CD-RW disc', 5000)
SELECT * FROM Customer
SELECT City, Country FROM Customer
SELECT * FROM Customer WHERE City = 'Kampala'
UPDATE Item SET Quantity = Quantity + 1 WHERE InvoiceNumber =
'3456'
DELETE FROM Customer WHERE City = 'Bukoba'
COUNT(*), AVG(Price)
SELECT StaffName, DepartmentName
FROM Staff, Department
WHERE Staff.Department = Department.ID
Type | Examples | Advantages | Disadvantages |
---|---|---|---|
Commercial | Oracle, DB2, Informix, Sybase, Microsoft SQL server | powerful, fast | complex, proprietary, expensive |
Open-source | PostgreSQL, MySQL, mSQL, Interbase | powerful, fast, free | difficult to install |
Java | Cloudscape, InstantDB, Pointbase | small, easy to install and use | not powerful, not fast |
Desktop | Paradox, Access | popular, easy to install and use, GUI | proprietary, not powerful, not cheap |
java -classpath driver_class_path;.
testprogram
Connection conn = DriverManager.getConnection(url, username,
password);
conn.close();
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE Test (Name CHAR(20))");
stat.execute("INSERT INTO Test VALUES ('UMU')");
String queryStatement = "SELECT * FROM Test";
ResultSet result = stat.executeQuery(queryStatement);
UPDATE
statements, use executeUpdate()
, or
execute()
and getResultSet()
or getUpdateCount()
.Statement
only has one ResultSet
.ResultSet
before making a new query on the same
Statement
. result.close();
Statement
when done with it. stat.close();
while (result.next()) {
... }
result.getString(
column number)
or result.getString(
column name)
.ResultSetMetaData metaData = result.getMetaData();
metaData.getColumnLabel(i)
is the name of column i
.getColumnDisplaySize(i)
is the column width.