1.注册驱动:
Class.forName("com.mysql.jdbc.Driver");显示的加载到JVM中
2.获取连接:
1.要连接数据库的url---- String url="jdbc:mysql://localhost:3306/test?"+ "useUnicode=true&characterEncoding=UTF8";//防止乱码
2.要连接数据库的用户名---- String user="xxxx";
3.要连接数据库的密码---- String pass="xxxx";
//DriverManager下的方法:getConnection(String url,String username,String password)
Connection conn=DriverManager.getConnection(url,user,pass);
3.创建一个Statement语句对象
//Connection接口下的方法:Statement createStatement()
Statement stmt=conn.createStatement();
PreparedStatement pstmt = conn.PreparedStatement() ;
CallableStatement cstmt = conn.prepareCall("{CALL demoSp(? , ?)}") ;
4.执行SQL语句:
除了查询语句是executeQuery();其他全部是executeUpdate();
ResultSet rs=stmt.executeQuery(Sql);
5.处理结果集:
ResultSet对象的getXxxx方法,取决于数据库中表的字段的类型,例如:varchar2 对应方法是getString ,如果是 integer 对应方法是getInt/getLong
6.关闭资源:
操作完以后要关闭jdbc来释放jdbc资源。但是顺序要和你定义对象的时候相反,就像关门一样,从里面先关,一直往外关。