日期:2014-05-17  浏览次数:20731 次

jsp数据库
哪个兄弟能给我一段连接数据库的代码,我弄了几次都有问题

------解决方案--------------------
哪个数据库?
------解决方案--------------------
Class.forName("oracle.jdbc.driver.OracleDriver");
url = "jdbc:oracle:thin:@localhost:端口:服务名";
conn = DriverManager.getConnection(url,"username","password");
stmt = conn.createStatement();
rs = stmt.executeQuery("sql");
------解决方案--------------------
sql2005连接数据库的:
import java.sql.*; 
public class Test {
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=test"; //连接服务器和数据库test
String userName = "sa"; //默认用户名
String userPwd = "123456"; //密码
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}