Created
July 23, 2019 13:42
-
-
Save gunaevart/0a486eab14ec58f1971f1d72a32a9f4a to your computer and use it in GitHub Desktop.
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 javaapp; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import java.sql.*; | |
public class Connect { | |
public static void main(String arg[]) | |
{ | |
Connect c1 = new Connect(); | |
c1.Conn(); | |
} | |
void Conn() | |
{ | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); | |
// Создание Statement для отправки запроса базе данных | |
Statement st = con.createStatement(); | |
// Результирующий запрос | |
ResultSet re = st.executeQuery("select * from user"); | |
while (re.next()) { | |
String login = re.getString("login"); | |
System.out.println(login); | |
} | |
System.out.println("Подключение успешно"); | |
} catch (ClassNotFoundException ex) { | |
Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex); | |
} catch (SQLException ex) { | |
Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment