Java uses SQL database code

Source: Internet
Author: User

Import java.sql.*;


public class SqlHelper {

Test connection function
public static void Main (String []args)
{
Derbyhelper de=new derbyhelper ();
String sql= "SELECT * from users";

List<object[]> list=de.querylist (SQL, NULL);
for (int i=0;i<list.size (); i++)
{Object []obj=list.get (i);
System.out.println (obj[1].tostring () + "" +obj[2].tostring ());
}

}


String driver= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url= "Jdbc:sqlserver://192.168.1.109:142;databasename=qqdb";
String url= "Jdbc:sqlserver://10.62.149.195:142;databasename=qqdb";

String user= "SA";
String passwd= "bbc223344";
Connection CT;
PreparedStatement PS;
ResultSet rs;
Public SqlHelper ()
{
try {
Class.forName (driver);
Ct=drivermanager.getconnection (URL,USER,PASSWD);

} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public int Update (String sql,string []paras)
{
int count=0;
try {
Ps=ct.preparestatement (SQL);
for (int i=0;i<paras.length;i++)
{
Ps.setstring (i+1, paras[i]);
}
Count=ps.executeupdate ();

} catch (SQLException e) {
TODO auto-generated Catch block
if (E.geterrorcode () ==2627)
count=2;//Insert Duplicate build.

E.printstacktrace ();
}finally{
This.close ();
}



return count;
}

Public ResultSet query (String sql,string[] paras)
{
try {
Ps=ct.preparestatement (SQL);
for (int i=0;i<paras.length;i++)
{
Ps.setstring (i+1, paras[i]);
}
Rs=ps.executequery ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
Not off.
}


Return RS;
}
public void Close ()
{

try {
if (rs!=null) rs.close ();
if (ps!=null) ps.close ();
if (ct!=null) ct.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
}

Java uses SQL database code

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.