SampleManager.java
Dosyayı İndir
package com.godoro.em;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class SampleManager {
public boolean delete(long sampleId) throws Exception {
Connection connection = DatabaseUtilities.getConnection();
String sql = "delete from Sample where sampleId=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setLong(1, sampleId);
int affected = statement.executeUpdate();
connection.close();
return affected > 0;
}
public Sample find(long sampleId) throws Exception {
Sample sample = null;
Connection connection = DatabaseUtilities.getConnection();
String sql = "select * from Sample where sampleId=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setLong(1, sampleId);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String sampleName = resultSet.getString("sampleName");
double sampleValue = resultSet.getDouble("sampleValue");
sample = new Sample(sampleId, sampleName, sampleValue);
}
connection.close();
return sample;
}
public boolean insert(Sample sample) throws Exception {
Connection connection = DatabaseUtilities.getConnection();
String sql = "insert into Sample(sampleName,sampleValue) values(?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, sample.getSampleName());
statement.setDouble(2, sample.getSampleValue());
int affected = statement.executeUpdate();
connection.close();
return affected > 0;
}
public boolean update(Sample sample) throws Exception {
Connection connection = DatabaseUtilities.getConnection();
String sql = "update Sample set sampleName=?,sampleValue=? where sampleId=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, sample.getSampleName());
statement.setDouble(2, sample.getSampleValue());
statement.setLong(3, sample.getSampleId());
int affected = statement.executeUpdate();
connection.close();
return affected > 0;
}
public List<Sample> list() throws Exception {
List<Sample> sampleList = new ArrayList<Sample>();
Connection connection = DatabaseUtilities.getConnection();
String sql = "select * from Sample";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
long sampleId = resultSet.getLong("sampleId");
String sampleName = resultSet.getString("sampleName");
double sampleValue = resultSet.getDouble("sampleValue");
Sample sample = new Sample(sampleId, sampleName, sampleValue);
sampleList.add(sample);
}
connection.close();
return sampleList;
}
}
Dosyayı İndir