JSP连接数据库的过程?
package data;
import java.sql.*;
public class DBConnection {
private String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; //"oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sdiedata"; //"jdbc:oracle:thin:@127.0.0.1:1521:oracle";
private String user = "sa";
private String password = "123";
public void setDriverName(String newDriverName) {
driverName = newDriverName;
}
public String getDriverName() {
return driverName;
}
public void setUrl(String newUrl) {
url = newUrl;
}
public String getUrl() {
return url;
}
public void setUser(String newUser) {
user = newUser;
}
public String getUser() {
return user;
}
public void setPassword(String newPassword) {
password = newPassword;
}
public String getPassword() {
return password;
}
public Connection getConnection() {
try {
Class.forName(driverName);
return DriverManager.getConnection(url, user, password);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
//DBConnectionManager DBConnectionManager1 = new DBConnectionManager();
try{
DBConnection dcm = new DBConnection();
Connection conn = dcm.getConnection();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from my_Book");
// stmt = conn.createStatement();
while(rs.next()){
System.out.print("名称: "+rs.getString(1));
System.out.print("\tpassword: "+rs.getString(2));
System.out.println();
}
}catch(Exception e){
e.printStackTrace();
}
}
}