budeme potrebovat mysql ovladac pro JDBC
![smile :)](./images/smilies/smile.gif)
![smile :)](./images/smilies/smile.gif)
a samotnej kod uz je pak easy:
- Kód: Vybrat vše
package basic.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class BasicConnect {
public static void main(String[] args) {
try { // pokud tohle hodi chybu, neudelali jste to spravne s tim connectorem :)
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
System.out.println("Chyba driveru");
System.out.println(e.toString());
}
try {
// pripojeni k db
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jmeno_db","username","heslo");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("Select * from table");
ResultSetMetaData meta = rs.getMetaData();
while (rs.next()) {
for (int i = 1; i <= meta.getColumnCount(); i++) {
System.out.print(meta.getColumnName(i) + ":");
System.out.println(rs.getString(i));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Pro rejpaly: tohle neni dalsi cast tutorialu !
Jen sem si s tim ted hral a zkousel, tak to sem dam nez to zapomenu
![tongue :P](./images/smilies/tongue.gif)
Nekdy priste se podivam na nejaky Hibernate nebo neco podobnyho pro praci s DB
![smile :)](./images/smilies/smile.gif)