2. Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","jb123456");
3. Statement st=con.createStatement();
2. String url="jdbc:mysql://localhost:3306/test";
3. String user="root";
4. String psw="jb123456";
5. Connection con=DriverManager.getConnection(url,user,psw);
使用返回的 Statement 对象创建的结果集在默认情况下类型为 TYPE_FORWARD_ONLY,并带有 CONCUR_READ_ONLY 并发级别。已创建结果集的可保存性可调用 getHoldability() 确定。
为什么说很自然呢,因为无论是网上还是书本教程上得例子都是这样的,而且程序也确实正常运行了,于是大家也就心安理得的找葫芦画瓢下去了。
- com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
- //or:
- //new com.mysql.jdbc.Driver();
- String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";
- String user = "";
- String psw = "";
- Connection con = DriverManager.getConnection(url,user,psw);
-
- com.mysql.jdbc.Driver driver = null;
- //or:
- ClassLoader cl = new ClassLoader();
- cl.loadClass("com.mysql.jdbc.Driver");
哦,原来是com.mysql.jdbc.Driver在装载完后自动帮我们完成了这一步骤。源代码是这样的:
- package com.mysql.jdbc
-
- public class Driver extends NonRegisteringDriver implements java.sql.Driver {
- // ~ Static fields/initializers
- // --------------------------------------------- //
- // Register ourselves with the DriverManager
- //
- static {
-
t ry { -
java.sql.DriverManager.registerDriver(new Driver()); -
} catch (SQLException E) { -
throw new RuntimeException("Can't register driver!"); -
} -
} - // ~ Constructors
- // -----------------------------------------------------------
-
- public Driver() throws SQLException {
-
// Required for Class.forName().newInstance() - }
- }