Java Connection SQL2008 (query, insert, call stored procedure)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.