Link Database Code
Import java.sql.*;
public class Connect
{
Static Connection Conn;
static Statement SQL;
Static ResultSet Res;
Static String drivername= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
Static String dburl= "Jdbc:sqlserver://127.0.0.1:1433;integratedsecurity=true;"
dburl=dburl+ "Databasename= (here is the name of the database you want to connect to)";
public static Connection GetConnect ()
{
try{
Class.forName (drivername);
Conn=drivermanager.getconnection (Dburl);
System.out.println ("SQLSERVER2008 Connection success!");
}catch (Exception e)
{
E.printstacktrace ();
}
Return conn;
}
Execute the statement of adding, deleting and modifying
The public static int exesqloper (String strSQL)//method is called directly in other functions.
{
int intreturn=0;
try{
Conn=getconnect ();
Sql=conn.createstatement ();
Intreturn=sql.executeupdate (strSQL);
Conn.close ();
}catch (Exception e)
{
System.out.println (E.tostring ());
}
return intreturn;
}
Executing a SELECT statement
public static ResultSet Exesqlquery (String strSQL)//is also directly invoked using the
{
try{
Conn=getconnect ();
Sql=conn.createstatement ();
Res=sql.executequery (strSQL);
}catch (Exception e)
{
System.out.println (E.tostring ());
}
return res;
}
Main function
public static void Main (String arges[])
{
GetConnect ();
}
}