Import java.sql.*;
Import java.util.*;
public class SQL
{
Private Connection Conn;
Private String connstr, sqlstr;
Private String usename, PassWord;
Private PreparedStatement PS;
Private ResultSet RS;
CallableStatement Callproc
Private StringBuffer strbuf = new StringBuffer ();
Public Connection getconnection ()
{
Try
{
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); Database-driven loading
} catch (ClassNotFoundException e)
{
SYSTEM.OUT.PRINTLN ("Database driver loading failed! ");
}
Try
{
ConnStr = "Jdbc:sqlserver://localhost:1433;databasename=tmp"; To define a connection database URL
Usename = "sa";
PassWord = "";
conn = Drivermanager.getconnection (ConnStr, Usename, PassWord); Connecting to a database
IF (conn! = null)
SYSTEM.OUT.PRINTLN ("Database connection OK");
} catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("Database connection failed");
}
Return conn;
}
public void GetData ()//throws SQLException
{
Strbuf.delete (0, Strbuf.length ());
SQLSTR = "SELECT * from TableA";
Try
{
if (conn = = NULL | | conn.isclosed ())
conn = getconnection ();
} catch (SQLException E1)
{
E1.printstacktrace ();
}
Try
{
PS = conn.preparestatement (SQLSTR);
rs = Ps.executequery ();
while (Rs.next ())
{
Strbuf.append ("1"). Append (rs.getstring (1));
Strbuf.append ("1:"). Append (Rs.getstring ("A"));
Strbuf.append (rs.getstring ("A")). Append ("--")
. Append (Rs.getstring ("B")). Append ("--")
. Append (Rs.getstring ("C")). Append ("--")
. Append (Rs.getstring ("D"));
Strbuf.append ("\ r");
}
System.out.println (STRBUF);
System.out.println ("-----------------------");
} catch (SQLException e)
{
E.printstacktrace ();
} finally
{
Try
{
Rs.close ();
Ps.close ();
Conn.close ();
} catch (SQLException e)
{
E.printstacktrace ();
}
}
}
public void InsertData ()//Insert data, delete, update similarly
{
Strbuf.delete (0, Strbuf.length ());
Sqlstr = "INSERT into TableA (B, C, D) VALUES (?,?,?)";
Try
{
if (conn = = NULL | | conn.isclosed ())
conn = getconnection ();
} catch (SQLException E1)
{
E1.printstacktrace ();
}
Try
{
PS = conn.preparestatement (SQLSTR);
Ps.setstring (1, "B10");
Ps.setdouble (2, 10);
Ps.setstring (3, "D10");
Ps.executeupdate ();
System.out.println ("-----------------------");
} catch (Exception e)
{
E.printstacktrace ();
} finally
{
Try
{
Rs.close ();
Ps.close ();
Conn.close ();
} catch (SQLException e)
{
E.printstacktrace ();
}
}
}
public void Callproc ()//Call stored procedure with return value
{
Strbuf.delete (0, Strbuf.length ());
Sqlstr = "";
Try
{
if (conn = = NULL | | conn.isclosed ())
{
conn = getconnection ();
}
} catch (SQLException e)
{
E.printstacktrace ();
}
Try
{
CallableStatement Callproc = Conn.preparecall ("{Call Sp_tablea (?,?)}");
Callproc.setint (1, 4);
Callproc.registeroutparameter (2, Java.sql.Types.VARCHAR);
Callproc.execute ();
System.out.println (callproc.getstring (2));
System.out.println ("-----------------------");
} catch (SQLException e)
{
E.printstacktrace ();
}
}
public static void Main (string[] args)
{
SQL Sqldemo = new SQL ();
Sqldemo.getconnection ();
Sqldemo.getdata ();
Sqldemo.insertdata ();
Sqldemo.callproc ();
}
}
Java Connection SQL2008 (query, insert, call stored procedure)