Created
May 27, 2019 02:37
-
-
Save aminelch/6fa7d4fa086cb4832e1c903d163f559a to your computer and use it in GitHub Desktop.
simple class to comminucate with Mysql Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sqlDatabase; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
public class Main { | |
public static Connection getConnection() throws Exception { | |
try { | |
String driver = "com.mysql.cj.jdbc.Driver"; | |
String url = "jdbc:mysql://localhost:3306/java_miniprojet"; | |
String username = "aminelch"; | |
String password = "demo"; | |
Class.forName(driver); | |
Connection conn = DriverManager.getConnection(url, username, password); | |
System.out.println("Connected"); | |
return conn; | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
return null; | |
} | |
public static ResultSet listeUtilisateurs(Connection db) throws Exception { | |
String req = "SELECT * FROM users"; | |
Statement stm = db.createStatement(); | |
ResultSet res = stm.executeQuery(req); | |
return res; | |
} | |
public static void main(String[] args) throws Exception { | |
Connection mysqldb = getConnection(); | |
ResultSet utilisateurs = listeUtilisateurs(mysqldb); | |
while (utilisateurs.next()) { | |
System.out.println(utilisateurs.getString("nom")); | |
System.out.println(utilisateurs.getString("motdepasse")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment