Monday, 23 November 2015

MYSQL DB Connectivity with java program


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Second {

public static void main(String[] args) {
// TODO Auto-generated method stub
String query = "insert into sample values('sai123')";
Connection con = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/prasad",
"root", "root");
stmt = con.createStatement();
int i = stmt.executeUpdate(query);
System.out.println("No.of Inserted Records == > " + i);

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

try {
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

Output:-
No.of Inserted Records == > 1


No comments:

Post a Comment