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
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();
}
}
}
}
}
- 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();
- }
- }
- }
- }
- }