Advertisement
Guest User

Untitled

a guest
Jun 15th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. /**
  2.  *
  3.  * Erstellt von: philipp
  4.  * Erstellt am: 21.07.2010 um 20:59:37
  5.  * Projekt: mylab/mysql
  6.  *
  7.  */
  8. package mysql;
  9.  
  10. import java.sql.*;
  11.  
  12. public class FirstTest {
  13.  
  14.     static Connection con = null;
  15.     static Statement stmt = null;
  16.     static ResultSet rs = null;
  17.    
  18.     public static void main (String[] args) throws InstantiationException, IllegalAccessException, SQLException{
  19.        
  20.         try{
  21.              System.out.println("Create new driver instance ...");
  22.                 Class.forName("com.mysql.jdbc.Driver").newInstance();
  23.                 System.out.println("Created new driver instance!");
  24.                 System.out.println("Created new connection instance!");
  25.                 con = DriverManager.getConnection("jdbc:mysql://localhost/javatest", "mysql", "mysql");
  26.  
  27.         }
  28.         catch(ClassNotFoundException e){
  29.             e.getMessage();
  30.             System.exit(1);
  31.         }
  32.         try{
  33.             stmt = con.createStatement();
  34.             // Daten aus Tabelle bekommemn
  35.             rs = stmt.executeQuery("SELECT nr,name,sontiges FROM TEST_TABELLE");
  36.            
  37.             while (rs.next()){
  38.                 System.out.println("Nr: " + rs.getString(1));
  39.                 System.out.println("Name: " + rs.getString(2));
  40.                 System.out.println("Sonstiges: " + rs.getString(3));
  41.                 System.out.println("bla: " + rs.getString(1));
  42.             }
  43.             // Update der Daten
  44.             stmt.executeUpdate("INSERT INTO TEST_TABELLE (nr, name, sontiges) VALUES ('1', 'phil', 'if')");
  45.            
  46.            
  47.            
  48.            
  49.             stmt.close();
  50.             con.close();
  51.         }
  52.         catch(SQLException e){
  53.             e.getMessage();
  54.             return;
  55.                
  56.             }
  57.        
  58.         }
  59.        
  60.    
  61.    
  62.    
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement