Powered by Blogger.
Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Thursday, 18 September 2014

Sql


What's the difference between "PreparedStatement" and "Statement"?
PreparedStatements are useful when you have one query to execute several times with just parameters changed. In normal case each and every query has to be checked by database whether syntax is ok or not. SQL Statement are precomplied and stored in PreparedStatement object, so it saves time of database to check its syntax.
        The PreparedStatement is a slightly more powerful version of a Statement, and should always be at least as quick and easy to handle as a Statement.
  1. Parse the incoming SQL query
  2. Compile the SQL query
  3. Plan/optimize the data acquisition path
  4. Execute the optimized query / acquire and return data
A Statement will always proceed through the four steps above for each SQL query sent to the database. A PreparedStatement pre-executes steps (1) - (3) in the execution process above. Thus, when creating a PreparedStatement some pre-optimization is performed immediately. The effect is to lessen the load on the database engine at execution time.
The other strength of the PreparedStatement is that you can use it over and over again with new parameter values, rather than having to create a new Statement object for each new set of parameters. This approach is obviously more efficient, as only one object is created.
Use the set methods each time to specify new parameter values.

What information is needed to create a TCP Socket? 

The Local Systems IP Address and Port Number. And the Remote System’s IPAddress and Port Number.

What are stored procedures? How is it useful? 

A stored procedure is a set of statements/commands which reside in the database. The stored procedure is pre-compiled and saves the database the effort of parsing and compiling sql statements every time a query is run. Each database has its own stored procedure language, usually a variant of C with a SQL pre-processor. Newer versions of db’s support writing stored procedures in Java and Perl too. Before the advent of 3-tier/n-tier architecture it was pretty common for stored procedures to implement the business logic( A lot of systems still do it). The biggest advantage is of course speed. Also certain kind of data manipulations are not achieved in SQL. Stored procedures provide a mechanism to do these manipulations. Stored procedures are also useful when you want to do Batch updates/exports/houseKeeping kind of stuff on the db. The overhead of a JDBC Connection may be significant in these cases.

What Class.forName will do while loading drivers? 

It is used to create an instance of a driver and register it with the DriverManager. When you have loaded a driver, it is available for making a connection with a DBMS.

How to Retrieve Warnings? 
SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object. 

SQLWarning warning = stmt.getWarnings();
if (warning != null)
{
    while (warning != null)
      {
          System.out.println(\"Message: \" + warning.getMessage());
          System.out.println(\"SQLState: \" + warning.getSQLState());
          System.out.print(\"Vendor error code: \");
          System.out.println(warning.getErrorCode());
          warning = warning.getNextWarning();
  }

}

Difference between the following in java System.exit(0);System.exit(-1);System.exit(1);
Zero=Everything Okay
Positive=Something I expected to go wrong went wrong (bad command-line, can't find file, could not connect to server)

Negative=Something I didn't expect went wrong (system error - unanticipated exception - externally forced termination e.g. kill -9)


















Friday, 22 August 2014

MSSql Server Authentication

private final static String databaseName="Map";
private final static String url = "jdbc:sqlserver://localhost;database="+databaseName;
private final static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private final static String userName = "sa";
private final static String password = "password123";
public static Connection getConnection() throws Exception {
if (connection != null)
return connection;
else {
try {
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(url, userName, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}

MSSQL windows Authentication

Configuration for MSSQL windows Authentication
1)Java Code:-
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
connection = DriverManager.getConnection("jdbc:sqlserver://localhost;database=Map;integratedSecurity=true");

2) dll configuration
copy sqljdbc_auth.dll in java/jre/bin directory( which is available in sqljdbc_4.0.2206.100_enu.tar on microsoft site).
other wise below error will come
Aug 22, 2014 8:44:25 PM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path

com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication. ClientConnectionId:264724cd-4fcf-463e-bf64-6a550077eab2
       at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667)
       at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<init>(AuthenticationJNI.java:60)
       at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2229)
       at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
       at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
       at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
       at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
       at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
       at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
       at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
       at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
       at java.sql.DriverManager.getConnection(DriverManager.java:582)
       at java.sql.DriverManager.getConnection(DriverManager.java:207)
       at com.dao.DBUtility.getConnection(DBUtility.java:30)
       at com.dao.DataDao.<init>(DataDao.java:12)
       at com.servlet.MapServlet.getStates(MapServlet.java:39)
       at com.servlet.MapServlet.doGet(MapServlet.java:29)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
       at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
       at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.UnsatisfiedLinkError: no sqljdbc_auth in java.library.path
       at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
       at java.lang.Runtime.loadLibrary0(Runtime.java:823)
       at java.lang.System.loadLibrary(System.java:1028)
       at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<clinit>(AuthenticationJNI.java:35)
       ... 29 more
null
java.lang.NullPointerException
       at com.dao.DataDao.getState(DataDao.java:129)
       at com.servlet.MapServlet.getStates(MapServlet.java:40)
       at com.servlet.MapServlet.doGet(MapServlet.java:29)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
       at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
       at java.lang.Thread.run(Thread.java:662)

Monday, 18 August 2014

import csv data in Postgresql

1.     Create table in postgresql
CREATE TABLE zipcodes  (
zip char(5),
city varchar,
state char(5),
latitude double precision,
longitude double precision, 
timezone varchar, dst varchar);
2.     write import statement
COPY zipcodes FROM 'C:/ZipCodes.csv' DELIMITER ',' CSV;
3.     Sample data for csv
Ø     "00210","Portsmouth","NH","43.005895","-71.013202","-5","1"
Ø     "00211","Portsmouth","NH","43.005895","-71.013202","-5","1"
Ø     "00212","Portsmouth","NH","43.005895","-71.013202","-5","1"
Ø     "00213","Portsmouth","NH","43.005895","-71.013202","-5","1"
Ø     "00214","Portsmouth","NH","43.005895","-71.013202","-5","1"
Ø     "00215","Portsmouth","NH","43.005895","-71.013202","-5","1"
Ø     "00501","Holtsville","NY","40.922326","-72.637078","-5","1"
Ø     "00544","Holtsville","NY","40.922326","-72.637078","-5","1"

Recent Articles

© 2014 Learning Java. WP themonic converted by Bloggertheme9. Published By Gooyaabi Templates
TOP