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