求用jdbc 连接数据库的java代码(只连接部分即可)

mysql数据库!!
2024-12-02 06:29:50
推荐回答(4个)
回答1:

public Connection getConnection(){//事先导入驱动
try{
Class.forname("sun.jdbc.odbc.jdbcodbcDriver");//加载驱动
System.out.println("驱动加载完毕");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try{
Connecion con=DriverManager.getConnecion("jdbc:odbc:数据库名成","用户名","密码");//创建连接对象,即已经连接到数据库
system.out.println("连接成功!");
}
catch(exception e){
e.printStackTrace();
}
}

回答2:

public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
return conn;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

回答3:

String driverName = "oracle.jdbc.driver.OracleDriver";

String url = "jdbc:oracle:thin:@localhost:1521:ORACLE9I";

Class.forName(driverName);

Connection con = null;

con = DriverManager.getConnection(url,"shown","dbwork");

回答4:

oracle数据库
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@127.0.0.1:1521:orcl