经过多次测试终于成功连接mysql。。。
值得一提的是oracle,sqlservel,mysql连接时的url写法有些小小的区别
oracle:jdbc:oracle:thin:@128.0.46.22:1521:orcl.这个我是用hibernate配置的
sqlservel:jdbc:sqlserver://localhost:1433;database=db_qqzone
mysql:jdbc:mysql://localhost:3306/stums
再这中间主要遇到的问题是端口号后面sqlservel用分号隔开而mysql用的是/而且经过实验,mysql不能写成database=stums.而sqlservel是可以省略database=的
public class DBManager {
private final static String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private final static String URL="jdbc:sqlserver://localhost:1433;database=db_qqzone";
private final static String USERNAME="sa";
private final static String PWD="admin123";
static{
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getconn(){
Connection conn = null;
try {
conn = DriverManager.getConnection(URL,USERNAME,PWD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(ResultSet rs,Statement st,Connection conn){
try {
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}