Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package forelesning_1;
- // import db.ConnectToDB;
- import java.sql.*;
- public class FirstDBConnection {
- public static void main(String[] args) throws SQLException {
- String brukernavn = args[0];
- String passord = args[1];
- String url = "jdbc:mysql://mysql.nith.no/marste";
- Connection con = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- //System.out.println("A");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- try {
- con = DriverManager.getConnection(url, brukernavn, passord);
- System.out.println("Fikk kontakt");
- System.out.println(" ");
- System.out.println(" ");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Statement stmt = con.createStatement();
- ResultSet res = stmt.executeQuery("select * from bokliste");
- // Kun printe ut bøkene
- /*while (res.next()) {
- String fornavn = res.getString(2);
- System.out.println(fornavn);
- } */
- // Printe ut hele lista.
- while (res.next()) {
- for (int i = 1; i <= 5; i++) {
- System.out.print(res.getString(i) + "\t");
- }
- System.out.println();
- }
- System.out.println();
- System.out.println();
- ResultSet res2 = stmt.executeQuery("select * from varer");
- while (res2.next()) {
- for (int i = 1; i <= 4; i++) {
- System.out.print(res2.getString(i) + "\t");
- }
- System.out.println();
- }
- } // End of main
- } // end of class
Add Comment
Please, Sign In to add comment