一些常用的数据库连接方式。
mySQl:
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3307/数据库名";
String user="root";
String password="";
conn= DriverManager.getConnection(url,user,password);
st=conn.createStatement();
}
catch(Exception e)
{
e.printStackTrace();
}
SQLSERVER:
Connection conn=null;
Statement st=null;
ResultSet rs=null;
PreparedStatement upst = null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=数据库名";
String user="sa";
String password="";
conn= DriverManager.getConnection(url,user,password);
}
catch(Exception e)
{
e.printStackTrace();
}
ORACLE:
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); // 加载驱动类
// 注册JDBC驱动
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
String sourceURL = "jdbc:oracle:thin:@localhost::1521:orcl"; //localhost视具体情况需要改变
String user = "scott";//用户
String password = "tiger";//密码
con = DriverManager.getConnection(sourceURL,user,password); // 建立连接
}catch (Exception e){
e.printStackTrace();
}