Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. <input type="text" name="login" size="45" />
  2.  
  3. <input type="password" name="password" size="45" />
  4.  
  5. /STEP 1. Import required packages
  6. import java.sql.*;
  7.  
  8. public class JDBCExample {
  9. // JDBC driver name and database URL
  10. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  11. static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
  12.  
  13. // Database credentials
  14. static final String USER = "username";
  15. static final String PASS = "password";
  16.  
  17. public static void main(String[] args) {
  18. Connection conn = null;
  19. Statement stmt = null;
  20. try{
  21. //STEP 2: Register JDBC driver
  22. Class.forName("com.mysql.jdbc.Driver");
  23.  
  24. //STEP 3: Open a connection
  25. System.out.println("Connecting to a selected database...");
  26. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  27. System.out.println("Connected database successfully...");
  28.  
  29. //STEP 4: Execute a query
  30. System.out.println("Inserting records into the table...");
  31. stmt = conn.createStatement();
  32.  
  33. String sql = "INSERT INTO Registration " +
  34. "VALUES (100, 'Zara', 'Ali', 18)";
  35. stmt.executeUpdate(sql);
  36. sql = "INSERT INTO Registration " +
  37. "VALUES (101, 'Mahnaz', 'Fatma', 25)";
  38. stmt.executeUpdate(sql);
  39. sql = "INSERT INTO Registration " +
  40. "VALUES (102, 'Zaid', 'Khan', 30)";
  41. stmt.executeUpdate(sql);
  42. sql = "INSERT INTO Registration " +
  43. "VALUES(103, 'Sumit', 'Mittal', 28)";
  44. stmt.executeUpdate(sql);
  45. System.out.println("Inserted records into the table...");
  46.  
  47. }catch(SQLException se){
  48. //Handle errors for JDBC
  49. se.printStackTrace();
  50. }catch(Exception e){
  51. //Handle errors for Class.forName
  52. e.printStackTrace();
  53. }finally{
  54. //finally block used to close resources
  55. try{
  56. if(stmt!=null)
  57. conn.close();
  58. }catch(SQLException se){
  59. }// do nothing
  60. try{
  61. if(conn!=null)
  62. conn.close();
  63. }catch(SQLException se){
  64. se.printStackTrace();
  65. }//end finally try
  66. }//end try
  67. System.out.println("Goodbye!");
  68. }//end main
  69. }//end JDBCExample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement