Guest User

Untitled

a guest
Feb 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. java.sql.SQLException: No database selected
  2.  
  3. String url = "jdbc:mysql://localhost:3306/";
  4.  
  5. import java.sql.Connection;
  6. import java.sql.Statement;
  7. import java.sql.DriverManager;
  8. import java.sql.SQLException;
  9.  
  10.  
  11. public class MysqlSetUp{
  12.  
  13. private static final String EMPLOYEE_TABLE = "create table MyEmployees3 ( "
  14. + " id INT PRIMARY KEY, firstName VARCHAR(20), lastName VARCHAR(20), "
  15. + " title VARCHAR(20), salary INT )";
  16.  
  17. public static Connection getConnection() throws Exception {
  18. String driver = "org.gjt.mm.mysql.Driver";
  19. String url = "jdbc:mysql://localhost:3306/";
  20. String username = "root";
  21. String password = "";
  22. Class.forName(driver);
  23. Connection conn = DriverManager.getConnection(url, username, password);
  24. return conn;
  25. }
  26. public static void main(String args[]) {
  27. Connection conn = null;
  28. Statement stmt = null;
  29. try {
  30. conn = getConnection();
  31. stmt = conn.createStatement();
  32. stmt.executeUpdate("CREATE DATABASE Employess");
  33. stmt.executeUpdate(EMPLOYEE_TABLE);
  34. stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(100, 'A')");
  35. stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(200, 'B')");
  36. System.out.println("CreateEmployeeTableMySQL: main(): table created.");
  37. } catch (ClassNotFoundException e) {
  38. System.out.println("error: failed to load MySQL driver.");
  39. e.printStackTrace();
  40. } catch (SQLException e) {
  41. System.out.println("error: failed to create a connection object.");
  42. e.printStackTrace();
  43. } catch (Exception e) {
  44. System.out.println("other error:");
  45. e.printStackTrace();
  46. } finally {
  47. try {
  48. stmt.close();
  49. conn.close();
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55. }
Add Comment
Please, Sign In to add comment