Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class MySQLAccess {
- private Connection connect = null;
- private Statement statement = null;
- private PreparedStatement preparedStatement = null;
- private ResultSet resultSet = null;
- public void writeDataBase(Long ico) throws Exception {
- try {
- // toto načíta MySQL ovládač, každá DB má vlastný ovládač
- Class.forName("com.mysql.jdbc.Driver");
- // Nastavenie spojenia s DB
- connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=root&password=8671");
- statement = connect.createStatement();
- // pre vyobrazenie
- // resultSet = statement.executeQuery("select * from tssu.faktura");
- // writeResultSet(resultSet);
- preparedStatement = connect.prepareStatement("insert into rpvs.seller values (?)");
- // ALTER TABLE tablename AUTO_INCREMENT = 1
- // Parameters start with 1
- preparedStatement.setLong(1, ico);
- preparedStatement.executeUpdate();
- // preparedStatement = connect.prepareStatement("SELECT dodavatel, cislo, ico, predmet, cena from tssu.faktura");
- // resultSet = preparedStatement.executeQuery();
- } catch (Exception e) {
- throw e;
- } finally {
- close();
- }
- }
- //vypisanie prvkov databazy
- /* private void writeResultSet(ResultSet resultSet) throws SQLException {
- // ResultSet is initially before the first data set
- while (resultSet.next()) {
- // It is possible to get the columns via name
- // also possible to get the columns via the column number
- // which starts at 1
- // e.g. resultSet.getSTring(2);
- String user = resultSet.getString("dodavatel");
- Integer website = resultSet.getInt("cislo");
- System.out.println("User: " + user);
- System.out.println("Website: " + website);
- }
- }*/
- // You need to close the resultSet
- private void close() {
- try {
- if (resultSet != null) {
- resultSet.close();
- }
- if (statement != null) {
- statement.close();
- }
- if (connect != null) {
- connect.close();
- }
- } catch (Exception e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement