jsp用代码在程序中连接数据库。
File -> Import导入项目, 导入sqlserver的jdbc.jar驱动程序自己写jdbc连接数据库
1. Class.forName加载驱动
2. Connection con = DriverManager.getConnection()获取连接
3. Statement stat = con.createStatement()创建执行状态
。。。新建个项目阿,把jsp页面放到webroot下,连接数据库如果是刚开始学的话就用JDBC。
public static void main(String[] args) {
// TODO Auto-generated method stub
//com.microsoft.sqlserver.jdbc.SQLServerDriver
String dir = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//这个是sqlserver2005的链接
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=studb";
String name = "sa";
String password = "密码自己改";
Connection con = null;
Statement stm = null;
ResultSet rs = null;
try {
Class.forName(dir);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url,name,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stm=con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}