Question

***JAVA*** Connect to a database to extract the data. This does not need to be a...

***JAVA***

Connect to a database to extract the data. This does not need to be a real database. For this portion of your project, you are just writing the code to connect to a database. You are not actually connecting to any specific database. You must include all of the necessary code to read from and write to the database. Just be sure to comment out all of the code so you can still run your program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the Java code for connecting with the database and executing the query. testdatabse is a sample database name taken for demonstration, it could be changed with any other database name.

Code-


import java.sql.*;  
class MysqlCon{  
public static void main(String args[]){  
try{  
Class.forName("com.mysql.jdbc.Driver"); // Loaing the driver class file into the memory
Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:3306/tesdatabase","root","root"); // establishing connection
//here testdatabase is database name, root is username and password  
Statement stmt=con.createStatement(); // interacting with database using createStatement()
ResultSet rs=stmt.executeQuery("select * from student"); // Executing the query. student is a dummy // table.

while(rs.next())  

System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); // Fetching the records
con.close(); // Closing the connection
}catch(Exception e){ System.out.println(e);}  
}  
}  

Code screenshot-

import java.sql.*; class Mysqlcon( public static void main(String args[])t tryt Class.forName(com.mysql.jdbc.Driver); // Loaing the driver class file into the memory Connection con-DriverManager.getConnection( jdbc:mysql://localhost:3306/tesdatabase, root, root) // establishing connection /here testdatabase is database name, root is username and password Statement stmt-con.createstatement(); // interacting with database using createstatement () Resultset rs-stmt.executeQuery( select * from student); // Executing the query. //student is a dummy table. while(rs.next()) out.println(rs.getInt (1)++rs.getstring (2)+ +rs.getstring(3)) // Fetching the records con.close) I Closing the connection Jcatch(Exception e)t System.out.printin(e);)

Add a comment
Know the answer?
Add Answer to:
***JAVA*** Connect to a database to extract the data. This does not need to be a...
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
  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • Information About This Project             In the realm of database processing, a flat file is a...

    Information About This Project             In the realm of database processing, a flat file is a text file that holds a table of records.             Here is the data file that is used in this project. The data is converted to comma    separated values ( CSV ) to allow easy reading into an array.                         Table: Consultants ID LName Fee Specialty 101 Roberts 3500 Media 102 Peters 2700 Accounting 103 Paul 1600 Media 104 Michael 2300 Web Design...

  • *USE JAVA In this project you will create program that calculate the number of times every...

    *USE JAVA In this project you will create program that calculate the number of times every word appears in a given text file. Your program will take in the name of the file as well as the number of threads to create to speed up the process. The numbers of threads must be greater than 0. The output will be saved into a file in alphabetical order. Besides creating the program itself, you are to run experiments to show how...

  • java code. James needs to create a computer text based game where some user has to...

    java code. James needs to create a computer text based game where some user has to enter as many words with the given letters as the given word. A good start would be writing a method that checks if a method has the same letters as the other. include pseudo code+ Input-Listen and Silent have the same word that's all information I have is a Anagram program that need to be done.ASAP just help me with the pseudo code of...

  • C ++ Implement cat command The purpose of this assignment is to provide practice using the...

    C ++ Implement cat command The purpose of this assignment is to provide practice using the system calls we discussed for working with files on a UNIX system. You will be writing a basic implementation of the cat command using C++. Description As you should recall, the cat command takes a list of files as command line arguments. It then opens each file in turn, writing each file’s entire contents to standard output in the order they were supplied. You...

  • C ++ Implement cat command The purpose of this assignment is to provide practice using the...

    C ++ Implement cat command The purpose of this assignment is to provide practice using the system calls we discussed for working with files on a UNIX system. You will be writing a basic implementation of the cat command using C++. Description As you should recall, the cat command takes a list of files as command line arguments. It then opens each file in turn, writing each file’s entire contents to standard output in the order they were supplied. You...

  • Assignment Modify the code Binary File IO Example Code. The code reads and prints information abo...

    In java Assignment Modify the code Binary File IO Example Code. The code reads and prints information about a BMP file. Modify the code so that it also prints the resolution of the BMP file (both width and height) and the number of bits per pixel. Do not use any Java library or third-party library code that reads BMP files. You must read the file using simple Java code and extract the information, similar to the code in the example....

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

  • Help In Database: Next comes the data entry: Make up data for your database, or find...

    Help In Database: Next comes the data entry: Make up data for your database, or find real data. You can enter the data with INSERT statements, but it is much easier to use PhpAdmin. It has a form where you can type the data directly. Alternatively, you can use PhpAdmin to import CSV data (comma-separated values), which you can export from Excel. Include at least 3 rows in each of the entity relations, and as many rows as you need...

  • I need help creating a netbeans application and linking database with steps Part II – Next,...

    I need help creating a netbeans application and linking database with steps Part II – Next, build a simple Servlet called LoginServlet in your “Bank” Project. Now make it so that when the Customer logs in, the LoginServlet will get called and will validate the user id and password. At first, just make sure that the Servlet gets called correctly. So just print a simple message like “LoginServlet Running…”. Remember, to call the LoginServlet, you will need to modify the...

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