Package Test2;
Import java.sql.*;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
public class DB {
private static String driver= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url= "Jdbc:sqlserver://localhost:1433;database=studb";
private static String use= "SA";
private static String pwd= "123456";
private static Connection con;
public static Connection Getcon ()
{
try {
Class.forName (driver);
Con=drivermanager.getconnection (URL,USE,PWD);
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return con;
}
public static void CloseAll (ResultSet re,statement sta,connection con)
{
try {
if (re!=null)
{
Re.close ();
}
if (sta!=null)
{
Sta.close ();
}
if (con!=null)
{
Con.close ();
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
----------------------------------
Package Test2;
Import java.sql.*;
Import java.util.*;
public class Studb {
Private Connection Con=null;
Private PreparedStatement Sta=null;
Private ResultSet Re=null;
int result=0;
public int Updateset (String sql,list L)
{
try {
Con=db.getcon ();
Sta=con.preparestatement (SQL);
for (int i=0;i<l.size (); i++)
{
Sta.setobject (i+1, L.get (i));
}
Result=sta.executeupdate ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return result;
}
Public list<student> find (String sql)
{
List <Student> l=new arraylist<student> ();
try {
Con=db.getcon ();
Sta=con.preparestatement (SQL);
Re=sta.executequery ();
while (Re.next ())
{
String id=re.getstring (1);
String name=re.getstring (2);
String sex=re.getstring (3);
Student si=new Student (id,name,sex);
L.add (SI);
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return l;
}
}
---------------------------
Package Test2;
Import java.util.*;
public class Test {
public static void Main (string[] args) {
Studb s=new studb ();
List l=new ArrayList ();
String sql= "Delete from Useinfo where id=?";
L.add (1);
int re=s.updateset (SQL, L);
if (re>0)
{
System.out.println ("000cc");
}
String sql2= "Select *from useinfo";
List<student> L2=s.find (SQL2);
for (Student st:l2)
{
System.out.println (St.getid () +st.getname () +st.getsex ());
}
}
}
JDBC Connection and additions and deletions and lookups