Advertisement
Guest User

MySql

a guest
Jul 30th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package Funktion;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. public class MySql {
  9.  
  10. private static String username = "Darioko";
  11. private static String password = "";
  12. private static String database = "tablist";
  13. private static String host = "localhost";
  14. private static String port = "3306";
  15. private static Connection con;
  16.  
  17.  
  18.  
  19.  
  20.  
  21. public static void connect(){
  22. if(!isConnected()){
  23. try {
  24. con = DriverManager.getConnection("jdbc:mysql://" + host+":"+port+"/"+database,username,password);
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28.  
  29. }
  30. }
  31.  
  32. public static void close(){
  33. if(isConnected()){
  34. try {
  35. con.close();
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41.  
  42. public static boolean isConnected(){
  43. return con !=null;
  44. }
  45.  
  46. public static void createTable(){
  47. if(isConnected()){
  48. try {
  49.  
  50. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS Tablist (FUNC VARCHAR(100), String VARCHAR(100))");
  51. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS Farbrang (PG VARCHAR(100), String VARCHAR(100))");
  52.  
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58.  
  59. public static void update(String qry){
  60. if(isConnected()){
  61. try {
  62. con.createStatement().executeUpdate(qry);
  63. } catch (SQLException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. }
  68. public static ResultSet getResult(String qry){
  69. if(isConnected()){
  70. try {
  71. return con.createStatement().executeQuery(qry);
  72. } catch (SQLException e) {
  73. e.printStackTrace();
  74. }
  75.  
  76. }
  77. return null;
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement