Home > Java, Malo mešano > Java MySql connection (povezava)

Java MySql connection (povezava)

Copy mysql-connector-java-3114-bin to C:\Program Files\Java\jre1.6.X\lib\ext

JAVA:
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4.  
  5. // Notice, do not import com.mysql.jdbc.*
  6. // or you will have problems!
  7.  
  8. public class LoadDriver {
  9.  
  10.     Connection conn = null;
  11.    
  12.     public static void main(String[] args) {
  13.         try {
  14.         // The newInstance() call is a work around for some
  15.         // broken Java implementations
  16.  
  17.         Class.forName("com.mysql.jdbc.Driver").newInstance();
  18.         conn =
  19.         DriverManager.getConnection("jdbc:mysql://localhost/test?" +
  20.                                    "user=monty&password=greatsqldb");
  21.                                   
  22.         Statement stmt = null;
  23.         ResultSet rs = null;
  24.  
  25.         stmt = conn.createStatement();
  26.         rs = stmt.executeQuery("SELECT foo FROM bar");
  27.  
  28.         // or alternatively, if you don't know ahead of time that
  29.         // the query will be a SELECT...
  30.  
  31.         if (stmt.execute("SELECT foo FROM bar")) {
  32.             rs = stmt.getResultSet();
  33.         }
  34.  
  35.         } catch (Exception ex) {
  36.             // handle any errors
  37.             System.out.println("SQLException: " + ex.getMessage());
  38.             System.out.println("SQLState: " + ex.getSQLState());
  39.             System.out.println("VendorError: " + ex.getErrorCode());
  40.  
  41.         }
  42.         finally {
  43.         // it is a good idea to release
  44.         // resources in a finally{} block
  45.         // in reverse-order of their creation
  46.         // if they are no-longer needed
  47.  
  48.         if (rs != null) {
  49.             try {
  50.                 rs.close();
  51.             } catch (SQLException sqlEx) { } // ignore
  52.  
  53.             rs = null;
  54.         }
  55.  
  56.         if (stmt != null) {
  57.             try {
  58.                 stmt.close();
  59.             } catch (SQLException sqlEx) { } // ignore
  60.  
  61.             stmt = null;
  62.         }
  63.     }   
  64. }

JAVA:
  1. import java.sql.*;
  2.  
  3. public class Jdbc10 {
  4.   public static void main(String args[]){
  5.     System.out.println(
  6.                   "Copyright 2004, R.G.Baldwin");
  7.     try {
  8.       Statement stmt;
  9.       ResultSet rs;
  10.  
  11.       //Register the JDBC driver for MySQL.
  12.       Class.forName("com.mysql.jdbc.Driver");
  13.  
  14.       //Define URL of database server for
  15.       // database named JunkDB on the localhost
  16.       // with the default port number 3306.
  17.       String url =
  18.             "jdbc:mysql://localhost:3306/JunkDB";
  19.  
  20.       //Get a connection to the database for a
  21.       // user named auser with the password
  22.       // drowssap, which is password spelled
  23.       // backwards.
  24.       Connection con =
  25.                      DriverManager.getConnection(
  26.                         url,"auser", "drowssap");
  27.  
  28.       //Display URL and connection information
  29.       System.out.println("URL: " + url);
  30.       System.out.println("Connection: " + con);
  31.  
  32.       //Get a Statement object
  33.       stmt = con.createStatement();
  34.  
  35.       //As a precaution, delete myTable if it
  36.       // already exists as residue from a
  37.       // previous run.  Otherwise, if the table
  38.       // already exists and an attempt is made
  39.       // to create it, an exception will be
  40.       // thrown.
  41.       try{
  42.         stmt.executeUpdate("DROP TABLE myTable");
  43.       }catch(Exception e){
  44.         System.out.print(e);
  45.         System.out.println(
  46.                   "No existing table to delete");
  47.       }//end catch
  48.  
  49.       //Create a table in the database named
  50.       // myTable.
  51.       stmt.executeUpdate(
  52.             "CREATE TABLE myTable(test_id int," +
  53.                   "test_val char(15) not null)");
  54.  
  55.       //Insert some values into the table
  56.       stmt.executeUpdate(
  57.                 "INSERT INTO myTable(test_id, " +
  58.                     "test_val) VALUES(1,'One')");
  59.  
  60.       //Get another statement object initialized
  61.       // as shown.
  62.       stmt = con.createStatement(
  63.                ResultSet.TYPE_SCROLL_INSENSITIVE,
  64.                      ResultSet.CONCUR_READ_ONLY);
  65.  
  66.       //Query the database, storing the result
  67.       // in an object of type ResultSet
  68.       rs = stmt.executeQuery("SELECT * " +
  69.                 "from myTable ORDER BY test_id");
  70.  
  71.       //Use the methods of class ResultSet in a
  72.       // loop to display all of the data in the
  73.       // database.
  74.       System.out.println("Display all results:");
  75.       while(rs.next()){
  76.         int theInt= rs.getInt("test_id");
  77.         String str = rs.getString("test_val");
  78.         System.out.println("\ttest_id= " + theInt
  79.                              + "\tstr = " + str);
  80.       }//end while loop
  81.  
  82.       //Display the data in a specific row using
  83.       // the rs.absolute method.
  84.       System.out.println(
  85.                         "Display row number 2:");
  86.       if( rs.absolute(2) ){
  87.         int theInt= rs.getInt("test_id");
  88.         String str = rs.getString("test_val");
  89.         System.out.println("\ttest_id= " + theInt
  90.                              + "\tstr = " + str);
  91.       }//end if
  92.  
  93.       //Delete the table and close the connection
  94.       // to the database
  95.       stmt.executeUpdate("DROP TABLE myTable");
  96.       con.close();
  97.     }catch( Exception e ) {
  98.       e.printStackTrace();
  99.     }//end catch
  100.   }//end main
  101. }//end class Jdbc10

Categories: Java, Malo mešano Tags:
  1. No comments yet.
  1. No trackbacks yet.
101890 pages viewed, 155 today
53332 visits, 92 today
FireStats icon Powered by FireStats