Question: this is a java assignment Below is a simple JDBC program connecting to a SQL Server database. Modify this program to connect to a database
this is a java assignment
Below is a simple JDBC program connecting to a SQL Server database. Modify this program to connect to a database of your choice, SQL Server, MySQL, Oracle. Then demonstrate this successfully running program.
import java.sql.*;
public class JDBCExample {
private static String url;
public static void main(String args[])
{
try {
url="jdbc:sqlserver://localhost:1433;databaseName=DB1;user=??;password=??;";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
new JDBCExample (); }
catch (ClassNotFoundException e) { System.out.println(e); }
}
public JDBCExample()
{
try {
Connection connect = DriverManager.getConnection(url);
Statement select = connect.createStatement();
echo("Table: PEOPLE ");
printAllData(select);
select.close();
connect.close(); }
catch (SQLException e) { System.out.println(e); }
}
private void printAllData(Statement stmt)
{
try {
ResultSet result;
String s = "SELECT * FROM People";
result = stmt.executeQuery(s);
ResultSetMetaData rsmd = result.getMetaData();
int cols = rsmd.getColumnCount();
for (int i = 1 ; i
String colName = rsmd.getColumnLabel(i);
System.out.print(String.format("%-12s",colName));
}
echo("");
for (int i = 1 ; i
System.out.print(String.format("%-12s","----------"));
echo("");
while (result.next())
{
for (int j = 1; j
System.out.printf("%-12s",result.getString(j));
echo("");
}
echo ("");
} // try
catch (SQLException sql) { sql.printStackTrace(); }
}
private void echo(String mssg)
{
System.out.println(mssg);
} }

Table: PEOPLE First Last City ID Bridgeport 100001 Hartford 1 Bridgeport 100006 New York 100023 23 Adam Sam Mohamed Susan Jones James Aziz Johnson 100004 35
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
