1.Class.forName(“com.mysql.jdbc.Driver”含宴)
2.DriverManager.registerDriver(new com.mysql.jdbc.Driver())
3.System.setProperty(“jdbc.drivers”,”com.mysql.jdbc.Driver”)
添加完是这样的
添加方式:
如果添加成功,在java代码中或者jsp中写代码连接数据库,代码差不多。(jsp麻烦一点)
给你java的代码,有步骤:
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1、首先加载数据库驱动类 Class.forName
// 2、使用DriverManager创建数据库连接Connection
// 3、使用Connection创建数据库命令对象(prepared)Statement
// 4、处理Restult,如果执行select
// 5、释放资源
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
String url = "jdbc:sqlserver://localhost:1433databaseName=crm"
Connection con = null
Statement stmt = null
ResultSet rs = null
try {
con = DriverManager.getConnection(url,
"sa", ".")
stmt =
con.createStatement()
rs = stmt.executeQuery("select * from Cusinfo")
while (rs.next()) {
System.out.println(rs.getString("cusNo"))
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} finally {
if (rs != null) {
try {
rs.close()
} catch (SQLException e) {
// 液尘TODO Auto-generated catch block
e.printStackTrace()
}
}
if (stmt != null) {
try {
stmt.close()
} catch (SQLException e) {
// 卜友TODO Auto-generated catch block
闹弊禅 e.printStackTrace()
}
}
if (con != null) {
try {
con.close()
} catch (SQLException e) {
}
con = null
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)