Question

Consider the following class: import java.sql. ; public class ConnDBServer / public Connection con; public ConnDBServer() { t

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

//ConnDBServer class

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.ResultSet;

import java.sql.Statement;

public class ConnDBServer {

    private static String url = "jdbc:mysql://localhost:3306/prototypeeop";    

    private static String driverName = "com.mysql.jdbc.Driver";   

    private static String username = "root";   

    private static String password = "root";

    private static Connection con;

    private static String urlstring;

    public static Connection getConnection() {

        try {

            Class.forName(driverName);

            try {

                con = DriverManager.getConnection(urlstring, username, password);

            } catch (SQLException ex) {

                // log an exception. fro example:

                System.out.println("Failed to create the database connection.");

            }

        } catch (ClassNotFoundException ex) {

            // log an exception. for example:

            System.out.println("Driver not found.");

        }

        return con;

    }

}


//Main class

import java.sql.*;

public class Main {

    public static void main (String[] args) {

        Connection con = null;

        Statement stmt = null;

        ResultSet rs = null;

        String sql="";

        try {

            con = ConnDBServer.getConnection();

            stmt = con.createStatement();

            rs = stmt.executeQuery(sql);

        }catch(Exception ex) {

                System.out.println("Main.");

        }


    }

}

Add a comment
Know the answer?
Add Answer to:
Consider the following class: import java.sql. ; public class ConnDBServer / public Connection con; public ConnDBServer()...
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 got an issue where line 48 "The method setDeveloper(String) is undefined for the type dev_selects"...

    I got an issue where line 48 "The method setDeveloper(String) is undefined for the type dev_selects" Any help is appreciated import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Date; import gUI.newer_gui; public class dev_selects {    public static void main(String[] args) {        // TODO Auto-generated method stub        Connection conn = null;        Statement stmt = null;        String str1 = "select Developers from New_Products";        String...

  • JAVA sql Code an sql file that creates a 'Contractors' table. See the exercise below for...

    JAVA sql Code an sql file that creates a 'Contractors' table. See the exercise below for an example. Give your 'Contractors' table the following fields Id (integer, primary key) CompanyName (varchar, 30 characters) Phone(varchar, 12 characters) ContactName(varchar, 30 characters) Rating (integer) OutOfStateService (boolean) Also have the sql file create at least five records for the "Contractors" table. Example record: 1, Tom's Contracting, 555-555-5555, Tom Thumb, 81, true import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestDB...

  • I am working on this code and keep getting errors like "ERROR: Syntax error: Encountered "<EOF>"...

    I am working on this code and keep getting errors like "ERROR: Syntax error: Encountered "<EOF>" at line 1, column 169." and "ERROR: Table/View 'STUDENT' does not exist. ERROR: Table/View 'STUDENT' does not exist. ERROR: Table/View 'STUDENT' does not exist." I do not know why this isn't working. Here is my code: DTCCDatabase.java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * This program creates the CoffeeDB database. */ public class DTCCDatabase {    public static void main(String[] args)...

  • What is the output of the following question? import java.io.IOException; import java.util. EmptyStackException: public class newclass...

    What is the output of the following question? import java.io.IOException; import java.util. EmptyStackException: public class newclass public static void main(String[] args) try System.out.printf("%d", 1); throw (new Exception()); catch (IOException e) System.out.printf("%d", 2); catch(EmptyStackException e) System.out.printf("%d", 3); catch(Exception e) System.out.printf("%d", 4); } finally System.out.printf("%d", 5); Q3) (15 points) a.Write a JAVA program that reads an arra JAVA program that reads an array from input file and invo Sort and Max ,that sorts the elements of the array and s the elements...

  • Really need help with this. This is for my Advanced Java Programming Class. If anyone is...

    Really need help with this. This is for my Advanced Java Programming Class. If anyone is an expert in Java I would very much appreciate the help. I will provide the code I was told to open. Exercise 13-3   Use JPA to work with the library checkout system In this exercise, you'll convert the application from the previous exercise to use JPA instead of JDBC to access the database. Review the project Start NetBeans and open the project named ch13_ex3_library that's...

  • There is a problem in the update code. I need to be able to update one...

    There is a problem in the update code. I need to be able to update one attribute of the students without updating the other attributes (keeping them empty). package dbaccessinjava; import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; import java.sql.*; import java.io.*; import java.util.Scanner; public class DBAccessInJava { public static void main(String[] argv) {    System.out.println("-------- Oracle JDBC Connection Testing ------"); Connection connection = null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con= DriverManager.getConnection("jdbc:oracle:thin:@MF057PC16:1521:ORCL", "u201303908","pmu"); String sql="select * from student"; Statement st; PreparedStatement ps; ResultSet rs;...

  • Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public...

    Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public class PresidentsMain { public static void main(String[] args) { Map<String, String> PresidentsOfTheUnitedStates = new HashMap<String, String>(); PresidentsOfTheUnitedStates.put("George Washington", "Unaffiliated"); PresidentsOfTheUnitedStates.put("John Adams", "Federalist"); PresidentsOfTheUnitedStates.put("Thomas Jefferson", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("James Madison", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("James Monroe", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("John Quincy Adams", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("Andrew Jackson", "Democratic"); PresidentsOfTheUnitedStates.put("Martin Van Buren", "Democratic"); PresidentsOfTheUnitedStates.put("William Henry Harrison", "Whig"); PresidentsOfTheUnitedStates.put("John Tyler", "Whig");      } } } Extend given client class: Implement a static method called FilterMapByValue, that takes...

  • package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public...

    package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public static void main (String [] args)    {    // ============================================================    // Step 2. Declaring Variables You Need    // These constants are used to define 2D array and loop conditions    final int NUM_ROWS = 4;    final int NUM_COLS = 3;            String filename = "Input.txt";    // A String variable used to save the lines read from input...

  • What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class...

    What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...

  • Error: Main method not found in class ServiceProvider, please define the main method as: public static...

    Error: Main method not found in class ServiceProvider, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application This is the error im getting while executing the following code Can you modify the error import java.net.*; import java.io.*; public class ServiceProvider extends Thread { //initialize socket and input stream private Socket socket = null; private ServerSocket server = null; private DataInputStream in = null; private DataOutputStream out = null; private int...

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