Advertisement
Guest User

mio

a guest
Nov 23rd, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5.  
  6. public class DB {
  7.    
  8. private static DB instance = new DB();
  9. public static final String URL = "jdbc:mysql://127.0.0.1:3306/user";
  10. public static final String USER = "root";
  11. public static final String PASSWORD = "";
  12. public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
  13.  
  14. public DB() {
  15. try {
  16.             Class.forName(DRIVER_CLASS);
  17.         }
  18. catch (ClassNotFoundException e) {
  19.             e.printStackTrace();
  20.         }
  21.     }
  22. public Connection createConnection() {
  23.         Connection connection = null;
  24.  
  25.         try {
  26.           connection = DriverManager.getConnection(URL, USER, PASSWORD);
  27.         } catch (SQLException e) {
  28.             System.out.println("ERROR: Unable to Connect to Database.");
  29.         }
  30.         return connection;
  31.     }  
  32.  
  33.     public static Connection getConnection() {
  34.         return instance.createConnection();
  35.     }
  36.    
  37.     public void scrivi(String user,String pass,String email) throws SQLException
  38.     {
  39.     Connection connection = null;
  40.         Statement statement = null;
  41.         try {
  42.             connection = DB.getConnection();
  43.             statement = connection.createStatement();
  44.             statement.executeUpdate("INSERT into Users (user,password,email) values('"+user+"','"+pass+"','"+email+"')");    
  45.    
  46.        
  47.         } finally {
  48.             if (connection != null) {
  49.                 try {
  50.                     connection.close();
  51.                 } catch (SQLException e) {
  52.                     e.printStackTrace();
  53.                 }
  54.             }
  55.         }      
  56.     }
  57.    
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement