JDBC Connection and additions and deletions and lookups

Source: Internet
Author: User

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

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.