How data retrieve from Database (Mysql)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class Database{
public static void main (String [] args) throws SQLException {
// url (path and port) of database in the computer
String url="jdbc:mysql://localhost:3306/world";
// establish the connection with the database by giving user name "root"and passphrase (password) "1122"
Connection con= DriverManager.getConnection(url,"root","1122");
//to give the query to the database we must create a statement
Statement st = con.createStatement();
//to get the retrieved the form table we create a result set all the data is store the in the result set variable
ResultSet rs= st.executeQuery("Select * from country where code= \'Pak\'");
// loop is used to get the value from result set
while(rs.next()) {
String a= rs.getString(1);
System.out.println(a);
}
// after all activities the connections are close to protect the Database
con.close();
}
}
Comments
Post a Comment