放在方法里不能释放连接什么的吧!还是...import java.sql.*;public class SQLconnect { public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
// myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
conn = null;
st = null;
rs = null;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=temp";
String userName = "sa";
String userPwd = "sa";
Connection dbConn = null;
Statement stmt = null;
ResultSet rs = null; try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!");
} catch (Exception e) {
e.printStackTrace();
} try {
System.out.println("the DB you connect is " + dbConn.getCatalog());// the
// DB
// you
// connect
stmt = dbConn.createStatement();
rs = stmt.executeQuery("select id from temp");
while (rs.next()) {
// System.out.println(rs.getString("id"));
System.out.println(rs.getInt(1));
}
dbConn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
free(rs, stmt, dbConn);
}
}
}
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
// myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
conn = null;
st = null;
rs = null;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=temp";
String userName = "sa";
String userPwd = "sa";
Connection dbConn = null;
Statement stmt = null;
ResultSet rs = null; try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!");
} catch (Exception e) {
e.printStackTrace();
} try {
System.out.println("the DB you connect is " + dbConn.getCatalog());// the
// DB
// you
// connect
stmt = dbConn.createStatement();
rs = stmt.executeQuery("select id from temp");
while (rs.next()) {
// System.out.println(rs.getString("id"));
System.out.println(rs.getInt(1));
}
dbConn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
free(rs, stmt, dbConn);
}
}
}
public static void doSomeThing(int i) {
i = 0;
}
public static void doSomeThing1(String s) {
s = null;
}
public static void main(String args[]) { int n = 3;
String str = "string";
doSomeThing(n);
doSomeThing1(str);
System.out.println(n);
System.out.println(str);
}
}
得到3
string
public static void doSomeThing1(StringBuffer s) {
s.delete(0, 6);
}
public static void main(String args[]) { StringBuffer str = new StringBuffer("string");
doSomeThing1(str);
System.out.println(str);
}
int这种基本类型传的是值,怎么着都没用
你free方法里close用的是对象起作用
=null操作的是引用没作用
大家搞清楚JVM内存管理方面的问题,比如方法区,堆,Java栈等。这些问题就很好理解了。