java.sql.statement 是个接口.但是我们在使用时却可以直接实例化.
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
public class dbconn {
private Connection cn;
public dbconn() throws ClassNotFoundException, SQLException
{
DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","7720563");
}
public void sql() throws SQLException
{
Statement stm = cn.createStatement();
stm.execute("update students set name='wenjun',sexy='man'");
stm.executeUpdate("delete from students where id =1");
}
public void close() throws SQLException{
cn.close();
}
}
不是很了解.或许我太菜了.是不是在加载驱动的时候.把接口实现了?
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
public class dbconn {
private Connection cn;
public dbconn() throws ClassNotFoundException, SQLException
{
DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","7720563");
}
public void sql() throws SQLException
{
Statement stm = cn.createStatement();
stm.execute("update students set name='wenjun',sexy='man'");
stm.executeUpdate("delete from students where id =1");
}
public void close() throws SQLException{
cn.close();
}
}
不是很了解.或许我太菜了.是不是在加载驱动的时候.把接口实现了?
java.util.Properties info) throws SQLException {
// Gets the classloader of the code that called this method, may
// be null.
ClassLoader callerCL = DriverManager.getCallerClassLoader(); return (getConnection(url, info, callerCL));
}
总之一句话,你看看src中的源代码就知道了.
然后通过一个接口引用指向这个对象Statement stm = cn.createStatement();
你可以通过rtti得到这个对象的具体类System.out.println(stm);我的机器上打印出来结果是:
com.mysql.jdbc.Statement@bb7465
很显然这个对象是属于com.mysql.jdbc.Statement类的
Statement stm = cn.createStatement();它返回的是一个实现了Statement的类.