how to connect java with mysql database >>>>
import java.sql.*;
public class ConnectToDatabase {
public static void main(String[] args) throws SQLException {
// declare a connection by using Connection interface
Connection connection = null;
/* Create string of connection url within specified format with machine
name, port number and database name. Here machine name id localhost
and database name is test. */
String connectionURL = "jdbc:mysql://localhost:3306/test";
/*declare a resultSet that works as a table resulted by execute a specified
sql query. */
ResultSet rs = null;
// Declare statement.
Statement statement = null;
try {
// Load JDBC driver "com.mysql.jdbc.Driver" by newInstance() method.
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes
parameters of string type connection url, user name and password to
connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "root");
/* createStatement() is used for create statement object that is
used for sending sql statements to the specified database. */
statement = connection.createStatement();
/* executeQuery() method execute specified sql query. Here this query checks
number of rows present in table */
rs = statement.executeQuery("select* from testinfo");
int count = 0;
while(rs.next()) {
count++;
}
System.out.println("Number of Rows present in table : "+count);
/* Here sql query find the element from the table havinf minimum ID */
rs = statement.executeQuery("select min(ID) from testinfo");
rs.next();
// This sql query delete the row having minimum ID.
statement.executeUpdate("delete from testinfo where ID='"+rs.getInt(1)+"'");
System.out.println("Row is deleted successfully.");
/* Here this query checks number of rows present in table after deletion */
int count1 = 0;
rs = statement.executeQuery("select* from testinfo");
while(rs.next()) {
count1++;
}
System.out.println("after deletion number of Rows present in table : "+count1);
// Here this query shows column names present in table.
rs = statement.executeQuery("show COLUMNS from testinfo");
System.out.println("Columns in table 'testinfo' of database 'test' : ");
while(rs.next()) {
System.out.println(rs.getString("Field"));
}
// This sql query delete the column name of specified name.
statement.executeUpdate("ALTER TABLE testinfo DROP Address");
System.out.println("column 'Address' is deleted successfully.");
// Again this query shows column names present in table.
rs = statement.executeQuery("show COLUMNS from testinfo");
System.out.println("after deletion columns in table 'testinfo' of database 'test' : ");
while(rs.next()) {
System.out.println(rs.getString("Field"));
}
}
// catch exceptions if found any exception at run time.
catch(Exception ex){
System.out.println("Sorry ! found some problems with database.");
System.out.println("Error is : "+ ex);
}
finally {
// close all the connections.
rs.close();
statement.close();
connection.close();
}
}
}
you better use netbeans for this.
in netbeans , click project tag and then right click on library tag now click add library find "MYSQL JDBC DRIVER"
then add.
now go to mysql and create a database and test run the application.
error : most of the time errors can be occurred because , the database not found (there is no database contain that or table(s) name or you don't have add that library.else there is so many critical status are there some times you have to reinstall the mysql sever.)