connect mysql code example
Example 1: java connect to mysql
import java.sql.*;
class MySqlConn{
public static void main(String args[]){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://
localhost:3306/databasename","username","password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from emp");
con.close();
}catch(Exception ex){
System.out.println(ex);
}
}
}
Example 2: mysql login console
mysql -u USERNAME -p
Example 3: mysql cli connect with password
mysql --user=root --password=mypass
Example 4: mysqli connect
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
?>
Example 5: mysql connect
$mysqli -> new mysqli(host, username, password, dbname, port, socket)
Example 6: login to database mysql terminal
use DBNAME;