import java.sql.*;
public class Conn
{
private static Connection con;
private Statement stmt;
private ResultSet rs;
private static final String drivername = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String url = "jdbc:microsoft:sqlserver://guoxuefeng:1433;DatabaseName=project1";
private static final String username = "sa";
private static final String userpass = "guoxuefeng";
// get the con of the database
private static synchronized Connection getCon() throws Exception
{
try
{
Class.forName(drivername);
con = DriverManager.getConnection(url,username,userpass);
return con;
}
catch(SQLException e)
{
throw e;
}
}
public Statement getStemtread()
{
try
{
con = getCon();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return stmt;
}
catch(Exception e)
{
//throw e;
}
return null;
}
public ResultSet getRs(String sql)
{
try
{
stmt = getStemtread();
rs = stmt.executeQuery(sql);
return rs;
}
catch(Exception e)
{
//throw e;
}
return null;
}
public Statement getStmt()
{
try
{
con = getCon();
stmt = con.createStatement();
return stmt;
}
catch(Exception e)
{
//throw e;
}
return null;
}
public synchronized void close()
{
try
{
if(rs!=null)
{
rs.close();
rs=null;
}
}
catch(Exception e)
{
//throw e;
}
try
{
if(stmt!=null)
{
stmt.close();
stmt = null;
}
}
catch(Exception e)
{
//throw e;
}
try
{
if(con!=null)
{
con.close();
con = null;
}
}
catch(Exception e)
{
//throw e;
}
}
public static void main(String[] args)
{
String t = "select * from NewsHead";
Conn con = new Conn();
try
{
ResultSet rs = con.getRs(t);
if(rs == null)
{
System.out.println("dadas");
return;
}
while(rs.next())
{
System.out.println(rs.getString(2));
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
}