Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class MysqlConnect {
- public static void main(String[] args) {
- System.out.println("MySQL Connect Example.");
- Connection conn = null;
- String url = "jdbc:mysql://localhost:3306/";
- String dbName = "pretos";
- String driver = "com.mysql.jdbc.Driver";
- String userName = "preto";
- String password = "preto";
- try {
- Class.forName(driver).newInstance();
- conn = DriverManager
- .getConnection(url + dbName, userName, password);
- System.out.println("Connected to the database");
- Statement s = conn.createStatement();
- s.execute("INSERT INTO `Pretos`.`pretos` (`Name`, `Age`) VALUES ('A', '80');");
- conn.close();
- System.out.println("Disconnected from database");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment