Question

10. a. Write a short Java Program that shows how SQL commands and JDBC can be used to connect the two. (See JDBC/ODBC Connect
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// import sql libraray
import java.sql.*;
class databaseConnection{
public static void main(String args[]){
try{
   // register the drivers
   Class.forName("com.mysql.jdbc.Driver");
   // create the connection instance
   // do fill the username and password,port number as of your database
   Connection con=DriverManager.getConnection("jdbc:mysql://localhost:portNumber","username","password");
   // create the statment to execute the query
   // different types of sql statements are avaiable
   // callable,prepared,statment
   Statement stmt=con.createStatement();
   // resultset instance to hold the result of query
   ResultSet rs=stmt.executeQuery("select * from emp");
   // rs.next is pointer the rows of result
   // we can move along each row by calling rs.next()
   while(rs.next())
   // getString ,getInt(),getDouble() are differet method to get the column value
   // so you must aware of the datatype of column otherwise exception is thrown
   System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getDouble(3));
   // colse the connection
   con.close();
   }
   catch(Exception e){
       // any wxception inside the class ishandled here
       System.out.println(e);
       }
   }
}

Sal Pnfection s an attack that at on tPthey it Corments Aynamic sl stulerent down me Sa) code (or adel statemnns B that dumys

Add a comment
Know the answer?
Add Answer to:
10. a. Write a short Java Program that shows how SQL commands and JDBC can be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the java program: A right triangle can have sides whose lengths are all integers. The...

    Write the java program: A right triangle can have sides whose lengths are all integers. The set of three integer values for the length of the sides of a triangle is called a Pythagorean triple. The length of the three sides must satisfy the relationship that the sum of the squares of the sides is equal to the square of the hypotenuse. Write a Java application that prompts the user for an integer that represents the largest side value and...

  • Java Description Write a program to compute bonuses earned this year by employees in an organization....

    Java Description Write a program to compute bonuses earned this year by employees in an organization. There are three types of employees: workers, managers and executives. Each type of employee is represented by a class. The classes are named Worker, Manager and Executive and are described below in the implementation section. You are to compute and display bonuses for all the employees in the organization using a single polymorphic loop. This will be done by creating an abstract class Employee...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

  • What an Executive Summary Is An executive summary is a specific type of document that does...

    What an Executive Summary Is An executive summary is a specific type of document that does two things: it summarizes a research article, and it offers recommendations as to how information from the article can be used. Some long reports can contain an executive summary section, as indicated in the Pearson handbook. Write a 2 pahe Executive Summary In business contexts, an executive summary is always written for a specific purpose: to explain the information in the article to a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT