JAVA – JSP
MySQL, JDBC, Select, Insert, Update,Delete
Bazy Danych
JDBC
• Narzędzia
• Połączenie z bazą danych
• Select
• Insert
• Update
• Delete
2
Narzędzia
• XAMP => MySql + PhpAdmin
– http://www.apachefriends.org/en/xampp.html – http://www.apachefriends.org/en/xampp-
windows.html#641
MySql JDBC Driver
• http://www.mysql.com/products/connector/
• http://dev.mysql.com/downloads/connector/j
4
Dodanie biblioteki do projektu
Ustawienie classpath dla serwera
6
Import class
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
Obiekty do obsługi operacji JDBC
private java.sql.Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
8
Połączenie z bazą danych
protected void LoadDataBase() { try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_test", "root","");
statement = connect.createStatement();
// resultSet zawiera wynik zapytania0 SQL
resultSet = statement .executeQuery("select * from java_test.osoba_test ");
} catch (ClassNotFoundException e) { e.printStackTrace();
}
catch (SQLException e) { e.printStackTrace();
} }
Baza danych
Brak klasy sterownika
użytkownik hasło
Błąd w
zapytaniu
Pobranie listy osób
public List<Osoba> GetOsobyFromJDBCDataBase(){
LoadDataBase();
List<Osoba> result = new ArrayList<Osoba>();
try {
while (resultSet.next()) {
String id = resultSet.getString("id"); // można podawać nr kolumny String imie = resultSet.getString("imie");
String nazwisko = resultSet.getString("nazwisko");
result.add(new Osoba(Integer.parseInt(id), imie, nazwisko) );
}
} catch (SQLException e) { e.printStackTrace();
}
return result;
}
10
Pobranie rekordu i umieszczenie w obiekcie (PreparedStatement)
public Osoba GetOsoba(int id){
Osoba result = new Osoba();
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager .getConnection("jdbc:mysql://localhost:3306/java_test" , "root","");
preparedStatement = connect
.prepareStatement("select * from java_test.osoba_test where id = ?");
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
resultSet.next();
result.setId( resultSet.getInt("id") );
result.setImie(resultSet.getString("imie"));
result.setNazwisko(resultSet.getString("nazwisko"));
} catch (…..) { ….}
return result;
}
Zapytanie
Ustawienie
parametrów
zapytania
Wykonanie
operacji
Insert
public void InsertOsoba(String imie, String nazwisko){
try {
preparedStatement = connect
.prepareStatement("insert into java_test.osoba_test (id, imie, nazwisko) values (default, ?, ?)");
preparedStatement.setString(1, imie);
preparedStatement.setString(2, nazwisko);
//preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11));
// preparedStatement.setInt ( …);
preparedStatement.executeUpdate();
} catch (SQLException e) { e.printStackTrace();
} }
12
Zapytanie
Ustawienie parametrów zapytania Inn typy
obiektów jako
parametry
zapytania
Wykonanie
operacji
Insert - Servelet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Osoby o = new Osoby();
o.LoadDataBase();
String imie = request.getParameter("Imie");
String nazwisko= request.getParameter("Nazwisko");
o.InsertOsoba(imie, nazwisko);
}
Edycja - update
public void UpdateOsoba(int id, String imie, String nazwisko){
try {
preparedStatement = connect
.prepareStatement("UPDATE java_test.osoba_test SET imie = ?, nazwisko= ? WHERE id= ?");
preparedStatement.setString(1, imie);
preparedStatement.setString(2, nazwisko);
preparedStatement.setInt(3, id);
preparedStatement.executeUpdate();
} catch (SQLException e) { }
}
14
Edycja – strona JSP
<%
pf.fdwp.Osoba osobaInp;
pf.fdwp.Osoby ososby= new pf.fdwp.Osoby();
pf.fdwp.Osoba o= ososby.GetOsoba(Integer.parseInt( request.getParameter("id") ));
osobaInp = o;
%>
<form action="EditOsoba" method="post">
Id:
<input name="Id" type="text" value='<%= osobaInp.getId() %>' />
Imie:
<input name="Imie" type="text" value='<%= osobaInp.getImie() %>'/>
Nazwisko:
<input name="Nazwisko" type="text" value='<%= osobaInp.getNazwisko() %>'/>
<button type="submit" >Apply</button>
</form>
Wyciągniecie danych na podstawie danych przesłanych w
requescie
Wypełnienie
Przejście do formularza z edycją
<table border="2">
<thead>
<tr><td> Id</td><td> Imie </td><td> Nazwisko</td></tr></thead>
<tbody>
<% for(Osoba o: osoby.GetOsobyFromJDBCDataBase()) { %>
<tr> <td><%= o.getId() %> </td> <td><%=o.getImie() %> </td> <td><%= o.getNazwisko() %> </td>
<td>
<a href="OsobaEdit.jsp?id=<%= o.getId() %>"> Edit</a>
</td>
</tr>
<% } %>
</tbody>
</table>
16