Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
plz suggest a link which will give a jdbc connection?
Posted

 
Share this answer
 
package apptest;

import java.sql.*;

public class AppTest {
// Code s7 Worked 

    public static void main(String[] args) {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://localhost:1433;"
                    + "databaseName=Northwind;user=sa;password=;";
            Connection con = DriverManager.getConnection(connectionUrl);
            Statement stmt = null;
            ResultSet rs = null;
            // SQL query command
             String SQL = "SELECT * FROM Products";
            stmt = con.createStatement();
            System.out.println("Connection is created...");

            rs = stmt.executeQuery(SQL);
            while (rs.next()) {

                System.out.println(rs.getString("ProductName") + " : " + rs.getString("UnitPrice"));
               
                System.out.println(" done");

            }

            con.close();
            System.out.println("Connection is closed...");

        } catch (SQLException e) {
            System.out.println("SQL Exception: " + e.toString());
        } catch (ClassNotFoundException cE) {
            System.out.println("Class Not Found Exception: " + cE.toString());
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900