Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.application.Application;
  4. import javafx.fxml.FXMLLoader;
  5. import javafx.scene.Parent;
  6. import javafx.scene.Scene;
  7. import javafx.stage.Stage;
  8. import javafx.stage.StageStyle;
  9.  
  10. import java.sql.*;
  11.  
  12. public class Main extends Application {
  13.  
  14.     @Override
  15.     public void start(Stage primaryStage) throws Exception{
  16.         Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
  17.         primaryStage.initStyle(StageStyle.UNDECORATED);
  18.         primaryStage.setTitle("Hello World");
  19.         primaryStage.setScene(new Scene(root));
  20.         primaryStage.show();
  21.     }
  22.     public static void createNewDatabase(String fileName) {
  23.  
  24.         String url = "jdbc:sqlite:C:/Users/danie/" + fileName;
  25.  
  26.         try (Connection conn = DriverManager.getConnection(url)) {
  27.             if (conn != null) {
  28.                 DatabaseMetaData meta = conn.getMetaData();
  29.                 System.out.println("The driver name is " + meta.getDriverName());
  30.                 System.out.println("A new database has been created.");
  31.             }
  32.  
  33.         } catch (SQLException e) {
  34.             System.out.println(e.getMessage());
  35.         }
  36.     }
  37.     public static void connect() {
  38.         Connection conn = null;
  39.         try {
  40.             // db parameters
  41.             String url = "jdbc:sqlite:C:/Users/danie/test.db";
  42.             // create a connection to the database
  43.             conn = DriverManager.getConnection(url);
  44.  
  45.             System.out.println("Connection to SQLite has been established.");
  46.  
  47.         } catch (SQLException e) {
  48.             System.out.println(e.getMessage());
  49.         } finally {
  50.             try {
  51.                 if (conn != null) {
  52.                     Statement statement = conn.createStatement();
  53.  
  54.                     ResultSet rs = statement.executeQuery("select * from person");while(rs.next())
  55.                     {
  56.                         // read the result set
  57.                         System.out.println("name = " + rs.getString("name"));
  58.                         System.out.println("id = " + rs.getInt("id"));
  59.                     }
  60.  
  61.                     conn.close();
  62.                 }
  63.             } catch (SQLException ex) {
  64.                 System.out.println(ex.getMessage());
  65.             }
  66.         }
  67.     }
  68.  
  69.  
  70.     public static void main(String[] args) {
  71.         //launch(args);
  72.         connect();
  73.        //createNewDatabase("test.db");
  74.  
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement