Download presentation
Presentation is loading. Please wait.
Published byLambert Boone Modified over 8 years ago
1
Web Programming Assistant Professor Xiaozhong Liu http://scholarwiki.indiana.edu/S517/S517.html
2
Compare Read from File Write to File FileReader file = new FileReader("test.txt"); Scanner s = new Scanner(reader); while( s.hasNext()) { System.out.println(s.next()); } s.close(); BufferedReader reader = new BufferedReader(file); FileWriter file = new FileWriter("test.txt"); writer.write(“Hello, I’m writing…”); writer.close(); BufferedWriter writer = new BufferedReader(file);
3
Servlet + File Compute test Please submit your information First number: Second number: web.html copy to project folder/WebContent/
4
Servlet + File protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = "/web.html"; request.getRequestDispatcher(path).forward(request, response); } load the html or jsp file from hard drive
5
Scope of variables public class test { String name; int time; public void connect () { int age; String name; for (int i = 0; i < 5; i++) { String bookname; }
6
Scope of variables public class test { String name; int time; public void connect () { int age; String name; for (int i = 0; i < 5; i++) { String bookname; } Class global variable: belongs to the class local variable: belongs to the method temp variable: belongs to the loop
7
Information problem… Input Output jTextField GUI Servlet File? Database? Internet? Error???
8
Database: Table Fields (of table)Type: String Type: int Data
9
Java open DB Java JDBC (Java Database Connectivity) Database Jar, complied Java classes
10
Java open DB
11
Java operates DB 1.Establish a connection (Connection class) 2.Create JDBC Statements (Statement class) 3.Execute SQL Statements (stmtement.executeUpdate…) 4.GET ResultSet (ResultSet class) 5.Close connections
12
Java – DB Connection import java.sql.*; Load the vendor specific driver Class.forName("oracle.jdbc.driver.OracleDriver"); // for Oracle database Class.forName("org.gjt.mm.mysql.Driver"); // for MYSQL database Make the connection Connection con = DriverManager.getConnection(…);
13
Java open DB Connection connection; Statement statement = null; Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://rdc04.uits.iu.edu:3264/S517DB"; String username = "S517"; String password = "S517"; connection = DriverManager.getConnection (url, username, password); statement = connection.createStatement();
14
Java open DB String url = "jdbc:mysql://rdc04.uits.iu.edu:3264/S517DB”; Server Address Port Number on DB server DB name
15
Java read from DB ResultSet rs = statement.executeQuery("select * from user"); while (rs.next()) { //Read each record int ID = rs.getInt("ID"); String firstname = rs.getString("Firstname"); String lastname = rs.getString("Lastname"); int age = rs.getInt("Age"); String gender = rs.getString("gender"); System.out.println(ID + "\t" + firstname + "\t"+ lastname + "\t" + age + "\t" + gender + "\t"); } rs.close(); statement.close(); connection.close();
16
Java read from DB rs.previous(); / / go back in the RS rs.relative(-5); / / go 5 records back rs.relative(7); / / go 7 records forward rs.absolute(100); / / go to 100th record
17
Java DB update Connection connection = DriverManager.getConnection (url, username, password); Statement statement = connection.createStatement(); statement.executeUpdate("insert into sometable…"); //shows how many rows are affected statement.close(); connection.close(); Insert, Delete, Update…
18
Basic DB search query Select * from user // Get all the record from user table Select Firstname, age, gender from user // Get Firstname, age, gender fields for all the record from user table Select * from user where age > 15 // Get all the record from user table where age larger than 15 Select * from user where Firstname = ‘Jenny’ // Get all the record from user table where Firstname is ‘Jenny’
19
DB Transaction Transaction control can be implemented by the Connection object, default mode is auto-commit, (each sql statement is treated as a transaction) We can turn off the auto-commit mode with con.setAutoCommit(false); Or turn on by con.setAutoCommit(true); If auto-commit is off, no SQL statement will be committed until - con.commit();
20
DB Metadata Statement stmt = conn.createStatement(); ResultSet resultSet = stmt.executeQuery("SELECT * FROM student"); ResultSetMetaData md = resultSet.getMetaData(); for (int i = 1; i <= md.getColumnCount(); i++) System.out.print(md.getColumnLabel(i) + " "); System.out.println(); while (resultSet.next()) { for (int i = 1; i <= md.getColumnCount(); i++) System.out.print(resultSet.getString(i) + " "); System.out.println(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.