public static void insert(String aName, double aSalary) throws SQLException {
PreparedStatement ps = null;
try {
ps = Database.getConnection().prepareStatement("INSERT INTO table (name, salary) VALUES (?, ?)");
ps.setString(1,aName);
ps.setDouble(2, aSalary);
ps.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public void insertData() throws SQLException {
EmployeeDAO.insert(this.name, this.salary);
}
Class.forName("org.postgresql.Driver");
Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres","postgres", "123456
");@WebServlet(
description = "generator servlet",
urlPatterns = {"/generator"})
public class Generator extends HttpServlet {
private static final Category LOG = Category.getInstance(Generator.class);
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = null;
try {
//Задаем тип страницы
response.setContentType("text/html");
//Для вывода
out = response.getWriter();
//Находим путь к файлу 'сonfig.properties'
Properties props = new Properties();
String propsFile = "config.properties";
InputStream input = getClass().getClassLoader().getResourceAsStream(propsFile);
//Если файл properties не найден
if (input == null) {
LOG.error("property file " + propsFile + " not found");
out.print("<h1>Sorry, config file not found!</h1>");
return;
}
props.load(input);
//Задаем переменным значения
String pr = props.getProperty("product");
String clientsCount = request.getParameter("numUser");
String otpCount = request.getParameter("numUtp");
String lices = request.getParameter("licesee");
String date = request.getParameter("data");
String certPeriod = request.getParameter("certPeriod");
String pass = props.getProperty("password");
String lic = props.getProperty("license");
//Задаем имя файла
String fileName = lices + "_" + clientsCount + "_" + otpCount + "_" + date + ".lic";
//Место хранения файла
String outFile = props.getProperty("outFile");
//Путь к ключу
String keyFile = props.getProperty("keyFile");
//Добавляем в массив данные
ArrayList<String> snlist = new ArrayList<>();
for (int i = 10; i < 10; i++) {
snlist.add(pr);
snlist.add(clientsCount);
snlist.add(otpCount);
snlist.add(lices);
snlist.add(date);
snlist.add(certPeriod);
snlist.add(outFile);
snlist.add(keyFile);
snlist.add(pass);
snlist.add(lic);
}
//Вставляем в config.properties данные
props.put("ActiveOtpCount", otpCount);
props.put("ActiveClientsCount", clientsCount);
props.put("Licensee", lices);
props.put("LicenseExpired", date);
props.put("CertificatesPeriod", certPeriod);
//Инициализация утилит
LicenseGenerator.init();
KeyStore ks = KeyStore.getInstance("AKS", AkSigProv.PROV_NAME);
ks.load(new FileInputStream(keyFile), pass.toCharArray());
String a = ks.aliases().nextElement();
PrivateKey pk = (PrivateKey) ks.getKey(a, pass.toCharArray());
X509Certificate cert = (X509Certificate) ks.getCertificate(a);
//Генерируем лиценцию, и записываем в файл
String x = LicenseGenerator.generateLicense(pr, snlist, props, cert, pk);
saveLicense(response, x, outFile, fileName);
} catch (Exception ex) {
LOG.error(response, ex);
} finally {
out.close();
}
}
public void saveLicense(HttpServletResponse response, String x, String outFile, String fileName)
throws IOException, ServletException {
PrintWriter out = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outFile + fileName);
fos.write(x.getBytes());
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", fileName);
response.setHeader(headerKey, headerValue);
out.write(x);
} catch (IOException ex) {
LOG.error(response, ex);
} finally {
out.close();
fos.close();
}
}
}
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
public class ChecksumHandler {
public static byte[] createChecksum(String filename, HashTypes hash) throws Exception {
InputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[1024];
MessageDigest complete = null;
if(hash == HashTypes.MD5) complete = MessageDigest.getInstance("MD5");
else if(hash == HashTypes.SHA1) complete = MessageDigest.getInstance("SHA1");
else if(hash == HashTypes.SHA256) complete = MessageDigest.getInstance("SHA-256");
else if(hash == HashTypes.SHA512) complete = MessageDigest.getInstance("SHA-512");
else {
fis.close();
return null;
}
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
return complete.digest();
}
public static String getChecksum(String filename, HashTypes hash) throws Exception {
byte[] b = createChecksum(filename, hash);
String result = "";
for (int i=0; i < b.length; i++) {
result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
}
private void actionAdd() {
JFrame parentFrame = new JFrame();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file to save");
URL verifiedUrl = verifyUrl(addTextField.getText());
if (verifiedUrl != null) {
int userSelection = fileChooser.showSaveDialog(parentFrame);
if (userSelection == JFileChooser.APPROVE_OPTION) {
// что здесь написать,чтобы сохранял файл куда я указал?
tableModel.addDownload(new Download(verifiedUrl));
addTextField.setText(""); // reset add text field
}
} else {
JOptionPane.showMessageDialog(this,
"Invalid Download URL", "Error",
JOptionPane.ERROR_MESSAGE);
}
}