JSP之JDBC操作Sql Server数据库
具体事例:
[java]
package Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
private static Connection con = null;
private static Statement statement = null;
private static ResultSet set = null;
private String sql = "";
// 加载SqlServer JDBC驱动
private static String driverNameOfSqlServer = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// IP地址(改为自己的IP地址)
private static String DatabaseIP = "localhost";
// 数据库用户名
private static String DatabaseUser = "sjf";
// 数据库密码
private static String DatabasePassword = "123456";
// 数据库名称
private static String DatabaseName = "pubs";
// URL
private static String DatabaseUrl = "jdbc:sqlserver://" + DatabaseIP + ":1433;DatabaseName = " + DatabaseName;
//获取一个数据库的连接
public Connection getConnection() {
try {
//注册驱动程序
Class.forName(driverNameOfSqlServer);
// 获取连接
con = DriverManager.getConnection(DatabaseUrl, DatabaseUser,DatabasePassword);
} catch (Exception e) {
System.out.println("getConnection出现错误");
e.printStackTrace();
}
return con;
}
//创建会话
public Statement getStatement(Connection con){
if(con != null){
try {
statement = con.createStatement();
return statement;
} catch (SQLException e) {
System.out.println("getStatement出现错误");
e.printStackTrace();
}
}
return null;
}
//查询
public ResultSet getResultSetQuery(Statement statement,String sql) {
if(statement != null){
try {
set = statement.executeQuery(sql);
return set;
} catch (SQLException e) {
System.out.println("getResultSetQuery出现错误");
e.printStackTrace();
}
}
return null;
}
//增加,修改,删除记录
public void getResultSetUpdate(Statement statement,String sql) {
if(statement != null){
try {
statement.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("getResultSetUpdate出现错误");
e.printStackTrace();
}
}
}
//关闭连接
public static void colse(Connection con){
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭会话
public static void close(Statement statement){
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭查询集
public static void close(ResultSet set){
if(set != null){
try {
set.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
private static Connection con = null;
private static Statement statement = null;
private static ResultSet set = null;
private String sql = "";
// 加载SqlServer JDBC驱动
private static String driverNameOfSqlServer = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// IP地址(改为自己的IP地址)
private static String DatabaseIP = "localhost";
// 数据库用户名
private static String DatabaseUser = "sjf";
// 数据库密码
private static String DatabasePassword = "123456";
// 数据库名称
private static String DatabaseName = "pubs";
// URL
private static String DatabaseUrl = "jdbc:sqlserver://" + DatabaseIP + ":1433;DatabaseName = " + DatabaseName;
//获取一个数据库的连接
public Connection getConnection() {
try {
//注册驱动程序
Class.forName(driverNameOfSqlServer);
// 获取连接
con = DriverManager.getConnection(DatabaseUrl, DatabaseUser,DatabasePassword);
} catch (Exception e) {
System.out.println("getConnection出现错误");
e.printStackTrace();
}
return con;
}
//创建会话
public Statement getStatement(Connection con){
if(con != null){
try {
statement = con.createStatement();
return statement;
} catch (SQLException e) {
System.out.println("getStatement出现错误");
e.printStackTrace();
}
}
return null;
}
//查询
public ResultSet getResultSetQuery(Statement statement,String sql) {
if(statement != null){
try {
set = statement.executeQuery(sql);
return set;
} catch (SQLException e) {
System.out.println("getResultSetQuery出现错误");
e.printStackTrace();
}
}
return null;
}
//增加,修改,删除记录
public void getResultSetUpdate(Statement statement,String sql) {
if(statement != null){
try {
statement.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("getResultSetUpdate出现错误");
e.printStackTrace();
}
}
}
//关闭连接
public static void colse(Connection con){
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭会话
public static void close(Statement statement){
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭查询集
public static void close(ResultSet set){
if(set != null){
try {
set.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
测试:
[java]
DB db = new DB();
Connection con = db.getConnection();
Statement statement = db.getStatement(con);
String sql = "select * from dbo.jobs";
ResultSet rs = db.getResultSetQuery(statement, sql);
try {
if(rs.next()){
System.out.println("fdfsdfsdff"+rs.getString("job_desc"));
}
} catch (SQLException e) {
e.printStackTrace();
} www.2cto.com
DB db = new DB();
Connection con = db.getConnection();
Statement statement = db.getStatement(con);
String sql = "select * from dbo.jobs";
ResultSet rs = db.getResultSetQuery(statement, sql);
try {
if(rs.next()){
System.out.println("fdfsdfsdff"+rs.getString("job_desc"));
}
} catch (SQLException e) {
e.printStackTrace();
}
上一篇: 利用DBMS_REDEFINITION包将非分区表转化成分区表
下一篇: storm集群架构
推荐阅读
-
JSP中操作数据库的常用SQL标签用法总结
-
SQL Server 数据库调整表中列的顺序操作方法及遇到问题
-
Heidisql如何连接sql server数据库并做增删改操作?
-
MyEclipse 配置SQL Server 2008数据库驱动操作步骤
-
jsp简单连接SQL Server2000数据库的方法
-
ASP.NET2.0数据库入门之SQL Server
-
数据库(SQL Server )经典例题(二):对S表、P表、J表、SPJ表的操作——单表查询
-
jdbc连接sql server2017进行简单的增、删、改、查操作
-
SQL Server温故系列(2):SQL 数据操作 CRUD 之简单查询
-
SQL Server温故系列(1):SQL 数据操作 CRUD 之增删改合