Object oriented analysis and design. To do: Come up with a way to access a datab
ID: 3826455 • Letter: O
Question
Object oriented analysis and design.
To do:
Come up with a way to access a
database using Java code.
the code must run and produce an output
I must use
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
To set up a connection to a database, the code is this:
Connection con = DriverManager.getConnection( host, username, password );import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
ETC.
To set up a connection to a database, the code is this:
Connection con = DriverManager.getConnection( host, username, password );
Explanation / Answer
________________
also change the table name in the select statement:
import java.sql.DriverManager; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { public static void main(String[] args) throws SQLException, ClassNotFoundException { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;user=test;password=test1;database=Test2"); System.out.println("test"); Statement sta = conn.createStatement(); String Sql = "select * from testing_table"; ResultSet rs = sta.executeQuery(Sql); while (rs.next()) { System.out.println(rs.getString("txt_title")); } } } ________________
The given code is for conecting to MS SQL db and fetching results from a table. You may replace the attributes in the jdbc connection string with your own :
localhost:1433
user=test;password=test1;database=Test2
also change the table name in the select statement:
testing_table
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.