Download 1 1. PhP Project Create a new PhP Project as shown below and

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1. PhP Project
Create a new PhP Project as shown below and click next
1
Choose Local Web Site (Apache 24 needs to be installed)
Project URL is http://localhost/projectName
Then, click next
We do not use PhP Framework. Click finish
Delete index.php as shown below
2
After you copy php files in Source Files (the source files are available in a zip folder on our web site),
right click the php project name, then select Properties. Type browse.php as Index File.
Make sure your new project file should be in the following folder as shown below
3
Make sure that project run configuration property should include proper project folder and index file (e.g.,
browse.php) as shown below.
4
JDBC (Java Database Connectivity-- – Connecting to DBMS)
Oracle JDBC Tutorial & Documentation
http://docs.oracle.com/javase/tutorial/jdbc/basics/
Connector-J, MySql JDBC Driver download page,
http://dev.mysql.com/downloads/connector/j/
JDBC Drivers
A JDBC driver is a software component enabling a Java application to interact with a database. To
connect with individual databases, JDBC (the Java Database Connectivity API) requires drivers for each
database. The JDBC driver gives out the connection to the database and implements the protocol for
transferring the query and result between client and database.
A connection pool contains a group of JDBC connections that are created when the connection pool is
registered. Connection pools use a JDBC driver to create physical database connections. Your application
borrows a connection from the pool, uses it, then returns it to the pool by closing it.
Java supports four types of JDBC drivers




JDBC-ODBC bridge plus ODBC driver: Java code access ODBC native binary drivers, ODBC
driver accesses databases
Native-API partly-Java driver: Java code accesses database specific native binary drivers
JDBC-Net pure Java driver: Java code accesses database via DBMS-independent net protocol
Pure Java driver
In a pure Java-based driver that communicates directly with vendor's database through socket connection.
This is the highest performance driver available for the database and is usually provided by the vendor
itself.
Type 4 driver is extremely flexible, you don't need to install special software on the client or server.
Further, these drivers can be downloaded dynamically.
5
MySQL's Connector/J driver is a Type 4 driver. Because of the proprietary nature of their network
protocols, database vendors usually supply type 4 drivers.
The JDBC API is comprised of two packages:


java.sql
javax.sql
The JDBC Interfaces
Driver
Connection
Connection
Statement
Statement
Statement
Statement
ResultSet
ResultSet
ResultSet
ResultSet
6
2. Create a simple Java application
Java “companyJDBC” Application (using JDBC)
Create a new Java application project as shown below
Add MySQL JDBC jar file as shown below as an example
7
/*****Java Source Code ****/
package companyjdbc;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
* @author G
*/
public class CompanyJDBC {
public static void main(String args[])
throws SQLException, IOException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Could not load the driver");
}
String DB_URL = "jdbc:mysql://localhost:3306/company";
String user = "root";
String pass = "nbuser";
Connection conn = DriverManager.getConnection(DB_URL, user, pass);
String query = "select fname, lname, salary from employee where ssn = ?";
PreparedStatement p = conn.prepareStatement(query);
String ssn = readEntry("Enter a Social Security Number: ");
p.clearParameters();
p.setString(1, ssn);
ResultSet r = p.executeQuery();
while (r.next()) {
String fname = r.getString(1);
String lname = r.getString(2);
double salary = r.getDouble(3);
System.out.println(fname + " " + lname + " $" + salary);
}
conn.close();
}
//readEntry function -- to read input string
static String readEntry(String prompt) {
try {
StringBuffer buffer = new StringBuffer();
System.out.print(prompt);
System.out.flush();
8
int c = System.in.read();
while (c != '\n' && c != -1) {
buffer.append((char) c);
c = System.in.read();
}
return buffer.toString().trim();
} catch (IOException e) {
return "";
}
}
}
3.
Simple JSP program
9
Delete index.html as shown below
Add a JSP page as shown below, and give it a name testDB.jsp
Move the mouse cursor inside the HTML body tag, and click Ctrl-Space, and select DBReport full down
option as shown below
10
Then fill out the pop-up dialog box as shown below
jdbc/company is JNDI data source name (this assumes that you already configured data source name and
connection pool in your application server such as glassfish server). See below
11
Change the following generated source code snippet as shown below
<sql:query var="result" dataSource="jdbc/company">
SELECT * from department
</sql:query>
<sql:setDataSource var="departments" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/company"
user="root" password="nbuser"/>
<sql:query dataSource="${departments}" var="result">
SELECT * from department;
</sql:query>
Right click the testDB.jsp and run the file, you will see the result as shown below
12
4. Simple database Application based on JSF framework, Java Persistence API, Enterprise Java
Beans, Context Dependency Injection Beans
Create a Maven Project (you can also create ant Web application) as shown below. Name the project as
“simpleComnayEditor”.
Right click the project name, and select properties, and then inject JSF framework to the maven Web
application as shown below.
13
Make sure company database connection is alive as shown below.
Right click the project name and “select entity classes from databases” as shown below
14
Choose new data source as shown below.
Fill out the pop-up menu as shown below
15
Choose database tables that you want to manage as shown below.
16
Add entity to the package name and make sure all the check boxes are properly checked (see below)
17
Right click the project name and select JSF pages from Entity Classes..... (see below)
Click “Add All” button, and make sure check box of the “Include Referenced Classes” is checked
Modify package names as you wish as shown below. Then click finish.
18
Now you can see 3 new packages are created by the IDE as shown below.
19
Right click the project name and select “Clean Build”. Note: maven project build command automatically
deploys the web application to the application server
20
After build the project, right click the project name and run the maven web project.
21
22