Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class JDBCMain {
- public static void main(String[] args) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- System.err.println("JDBC-ajurin lataus epäonnistui");
- System.exit(0);
- }
- Connection connection = null;
- final String URL = "jdbc:mysql://localhost/autokanta";
- final String USER = "olso";
- final String PASSWD = "olso";
- Statement statement = null;
- ResultSet rs = null;
- try {
- connection = DriverManager.getConnection(URL, USER, PASSWD);
- statement = connection.createStatement();
- int x = statement.executeUpdate("INSERT INTO ajoneuvo (rekno,merkki)"
- + "VALUES ('AKU-313', 'Hyrysysy')");
- System.out.println("Pitäisi tulla 1: " + x);
- rs = statement.executeQuery("SELECT * FROM ajoneuvo");
- while (rs.next()) {
- String rsRekno = rs.getString("rekno");
- String rsMerkki = rs.getString("merkki");
- System.out.println(rsRekno + " " + rsMerkki);
- }
- } catch (SQLException e) {
- do {
- System.err.println("Viesti: " + e.getMessage());
- System.err.println("Virhekoodi: " + e.getErrorCode());
- System.err.println("SQL-tilakoodi: " + e.getSQLState());
- } while (e.getNextException() != null);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (statement != null)
- statement.close();
- if (connection != null)
- connection.close();
- } catch (Exception e) {
- System.err.println("Resurssien vapautuksessa virhe");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement