java连接MySQl数据库实例代码
package com.abc.dao;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
public class basedao {
public connection getconn()
{
connection conn=null;
try {
class.forname("com.mysql.jdbc.driver");
} catch (classnotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
string url="jdbc:mysql://127.0.0.1:3306/user";
try {
conn=drivermanager.getconnection(url, "root", "");
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return conn;
}
public void closeall(resultset rs,statement stat,connection conn)
{
if(rs!=null)
try {
rs.close();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
if(stat!=null)
try {
stat.close();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
if(conn!=null)
try {
conn.close();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
}
推荐阅读