Guest User

Untitled

a guest
Sep 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class MysqlConnect {
  4.     public static void main(String[] args) {
  5.         System.out.println("MySQL Connect Example.");
  6.         Connection conn = null;
  7.         String url = "jdbc:mysql://localhost:3306/";
  8.         String dbName = "pretos";
  9.         String driver = "com.mysql.jdbc.Driver";
  10.         String userName = "preto";
  11.         String password = "preto";
  12.         try {
  13.             Class.forName(driver).newInstance();
  14.             conn = DriverManager
  15.                     .getConnection(url + dbName, userName, password);
  16.             System.out.println("Connected to the database");
  17.            
  18.             Statement s = conn.createStatement();
  19.             s.execute("INSERT INTO `Pretos`.`pretos` (`Name`, `Age`) VALUES ('A', '80');");
  20.             conn.close();
  21.             System.out.println("Disconnected from database");
  22.         } catch (Exception e) {
  23.             e.printStackTrace();
  24.         }
  25.     }
  26. }
Add Comment
Please, Sign In to add comment