How to connect java with server
How to connect java with sql
Her is Code :
import java.sql.Connection;import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MyPanel {
public static void main(String[] args) throws Exception {
// Step-1 Connect to the Database on MySQL server
String url = "jdbc:mysql://localhost:3306/world";
String userName = "root";
String password = "admin";
Connection conn = DriverManager.getConnection(url, userName, password);
// Step-2 Create a Statement and run SQL query
Statement stmt = conn.createStatement();
// ResultSet rs = stmt.executeQuery("SELECT Code, Name, Population FROM world.country");
ResultSet rs = stmt.executeQuery("SELECT Code, Name, Population FROM world.country where Code='PAK'");
// Step-3 Process the Result of the Query
while(rs.next()) {
String code = rs.getString(1);
String name = rs.getString(2);
String p = rs.getString(3);
System.out.println(code + ", " + name + ", " + p);
}
// Step-4 Close the Connection
conn.close();
}
}
Reviews:
Post a Comment