Advertisement
gurumutant

Koneksi dari Java ke MySQL via JDBC

Aug 23rd, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1.  
  2. package cobajavaswing;
  3.  
  4. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  5. import java.sql.Connection;
  6. import java.sql.SQLException;
  7.  
  8. public class Koneksi {
  9.     private static Connection connection;
  10.    
  11.     public static Connection getConnection(){
  12.         //pengecekan koneksi database
  13.         if (connection==null){
  14.             try{
  15.               String username = "rpl";
  16.               String password = "Cm9150oZzemHHojq";
  17.               String url = "jdbc:mysql://localhost:3306/rpl_perpustakaan";
  18.              
  19.               MysqlDataSource source = new MysqlDataSource();
  20.               source.setUser(username);
  21.               source.setPassword(password);
  22.               source.setURL(url);
  23.              
  24.               connection = source.getConnection();
  25.             } catch (SQLException ex){
  26.               System.out.println("Error koneksi database");  
  27.             }
  28.         }      
  29.         return connection;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement