Advertisement
Guest User

Untitled

a guest
May 1st, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.sql.*;
  4. import java.util.*;
  5.  
  6.  
  7.  
  8. class Test {
  9. public static void main(String[] args) {
  10. try{
  11. // Class.forName("com.mysql.jdbc.Driver");
  12. Connection con = connect(); //Connection con = DriverManager.getConnection("jdbc:mysql://localhost/employee:8889", "root", "root");
  13.  
  14. Statement stmt = (Statement) con.createStatement();
  15.  
  16.  
  17. int id = 2;
  18. String name = "mouhamad";
  19. int salary = 1000;
  20.  
  21.  
  22.  
  23.  
  24. //String insert = "INSERT INTO eep VALUES (" + id + ", '" + name + "', " + salary + ")";
  25. String create = "CREATE TABLE MyGuests (\n" +
  26. "id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n" +
  27. "firstname VARCHAR(30) NOT NULL,\n" +
  28. "lastname VARCHAR(30) NOT NULL,\n" +
  29. "email VARCHAR(50),\n" +
  30. "reg_date TIMESTAMP\n" +
  31. ")";
  32.  
  33.  
  34.  
  35.  
  36. //stmt.executeUpdate(insert);
  37. stmt.execute(create);
  38. }catch(Exception e) {
  39. System.err.println("error : "+e);
  40. }
  41. }
  42.  
  43.  
  44. public static Connection connect() {
  45. Connection conn = null;
  46.  
  47. try {
  48. String userName = "root";
  49. String password = "root";
  50. String url = "jdbc:mysql://localhost:8889/employee";
  51.  
  52. Class.forName("com.mysql.jdbc.Driver").newInstance();
  53. conn = DriverManager.getConnection(url, userName, password);
  54.  
  55. System.out.println("Database connection established");
  56.  
  57. } catch (Exception e) {
  58.  
  59. System.err.println("Cannot connect to database server");
  60. e.printStackTrace();
  61.  
  62. }
  63. return conn;
  64.  
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement