java accessing in wampserver

  • ultimate11
  • Student
  • Student
  • ultimate11
  • Posts: 86

Post 3+ Months Ago

guys how to add a value in wamp server mysql from java
here is the code in accessing and retrieving only a data from mysql..
I want to add in java from its running to mysql..this is just displaying
Code: [ Select ]
import java.sql.*;
import java.*;

public class Main {
    public static void main (String[] args) {
        Class driver;
        driver = null;

        
        try {
            driver = Class.forName("com.mysql.jdbc.Driver");
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        System.out.println("Found driver " + driver);
        
        Connection connection;
        connection = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","root","");
        }
        catch (SQLException e) {
            e.printStackTrace();
            return;
        }
        
        try {
            System.out.println("Established connection to " + connection.getMetaData().getURL());
        }
        catch (SQLException el) {
            el.printStackTrace();
        }
        
        Statement statement;
        statement = null;
        try {
            statement = connection.createStatement();
            statement.execute("SELECT * FROM table1");
            ResultSet resset = statement.getResultSet();
            while(resset.next())
            {
                System.out.print(resset.getRow());
                System.out.print("\n");
                System.out.println("Name:" + resset.getString("name"));
                System.out.println("Year Graduated:" + resset.getString("ygraduated"));
                System.out.println("Age:" + resset.getString("age"));
                System.out.print("\n");
                
            }
            resset.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            if (statement != null)
            {
                try {
                    statement.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null)
            {
                try {
                    connection.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  1. import java.sql.*;
  2. import java.*;
  3. public class Main {
  4.     public static void main (String[] args) {
  5.         Class driver;
  6.         driver = null;
  7.         
  8.         try {
  9.             driver = Class.forName("com.mysql.jdbc.Driver");
  10.         }
  11.         catch (ClassNotFoundException e) {
  12.             e.printStackTrace();
  13.             return;
  14.         }
  15.         System.out.println("Found driver " + driver);
  16.         
  17.         Connection connection;
  18.         connection = null;
  19.         try {
  20.             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","root","");
  21.         }
  22.         catch (SQLException e) {
  23.             e.printStackTrace();
  24.             return;
  25.         }
  26.         
  27.         try {
  28.             System.out.println("Established connection to " + connection.getMetaData().getURL());
  29.         }
  30.         catch (SQLException el) {
  31.             el.printStackTrace();
  32.         }
  33.         
  34.         Statement statement;
  35.         statement = null;
  36.         try {
  37.             statement = connection.createStatement();
  38.             statement.execute("SELECT * FROM table1");
  39.             ResultSet resset = statement.getResultSet();
  40.             while(resset.next())
  41.             {
  42.                 System.out.print(resset.getRow());
  43.                 System.out.print("\n");
  44.                 System.out.println("Name:" + resset.getString("name"));
  45.                 System.out.println("Year Graduated:" + resset.getString("ygraduated"));
  46.                 System.out.println("Age:" + resset.getString("age"));
  47.                 System.out.print("\n");
  48.                 
  49.             }
  50.             resset.close();
  51.         }
  52.         catch (SQLException e) {
  53.             e.printStackTrace();
  54.         }
  55.         finally {
  56.             if (statement != null)
  57.             {
  58.                 try {
  59.                     statement.close();
  60.                 }
  61.                 catch (SQLException e) {
  62.                     e.printStackTrace();
  63.                 }
  64.             }
  65.             if (connection != null)
  66.             {
  67.                 try {
  68.                     connection.close();
  69.                 }
  70.                 catch (SQLException e) {
  71.                     e.printStackTrace();
  72.                 }
  73.             }
  74.         }
  75.     }
  76. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 193 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.