JDBC in JSP for SQL Server database operations

Source: Internet
Author: User

Example: [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 = ""; // load the JDBC driver private static String driverNameOfSqlServer = "c Om. microsoft. sqlserver. jdbc. SQLServerDriver "; // ip address (changed to your own IP address) private static String DatabaseIP =" localhost "; // database username private static String DatabaseUser =" sjf "; // Database Password private static String DatabasePassword = "123456"; // database name private static String DatabaseName = "pubs"; // URL private static String DatabaseUrl = "jdbc: sqlserver: // "+ database IP +": 1433; DatabaseName = "+ DatabaseName ;/ /Obtain the Connection public Connection getConnection () {try {// register the driver Class. forName (driverNameOfSqlServer); // obtain the connection con = DriverManager. getConnection (DatabaseUrl, DatabaseUser, DatabasePassword);} catch (Exception e) {System. out. println ("getConnection error"); e. printStackTrace ();} return con;} // creates a session public Statement getStatement (Connection con) {if (con! = Null) {try {statement = con. createStatement (); return statement;} catch (SQLException e) {System. out. println ("getStatement error"); e. printStackTrace () ;}} return null ;}// query public ResultSet getResultSetQuery (Statement statement, String SQL) {if (statement! = Null) {try {set = statement.exe cuteQuery (SQL); return set;} catch (SQLException e) {System. out. println ("getResultSetQuery error"); e. printStackTrace () ;}} return null;} // Add, modify, and delete the public void getResultSetUpdate (Statement statement, String SQL) {if (statement! = Null) {try {statement.exe cuteUpdate (SQL);} catch (SQLException e) {System. out. println ("getResultSetUpdate error"); e. printStackTrace () ;}}// close the Connection to public static void colse (Connection con) {if (con! = Null) {try {con. close ();} catch (SQLException e) {e. printStackTrace () ;}}// close the public static void close (Statement statement) {if (statement! = Null) {try {statement. close ();} catch (SQLException e) {e. printStackTrace () ;}}// close the query set 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 = ""; // Load SqlServer JDBC driver private static String driverNameOfSqlServer = "com. microsoft. sqlserver. jdbc. SQLServerDriver "; // ip address (changed to your own IP address) private static String DatabaseIP =" localhost "; // database username private static String DatabaseUser =" sjf "; // Database Password private static String DatabasePassword = "123456"; // database name private static String DatabaseName = "pubs"; // URLprivate static String DatabaseUrl = "jdbc: sqlserver: // "+ DatabaseIP + ": 1433; DatabaseName =" + DatabaseName; // obtain the Connection public Connection getConnection () {try {// register the driver Class. forName (driverNameOfSqlServer); // obtain the connection con = DriverManager. getConnection (DatabaseUrl, DatabaseUser, DatabasePassword);} catch (Exception e) {System. out. println ("getConnection error"); e. printStackTrace ();} return con;} // creates a session public Statement getStatement (Connection con) {if (con! = Null) {try {statement = con. createStatement (); return statement;} catch (SQLException e) {System. out. println ("getStatement error"); e. printStackTrace () ;}} return null ;}// query public ResultSet getResultSetQuery (Statement statement, String SQL) {if (statement! = Null) {try {set = statement.exe cuteQuery (SQL); return set;} catch (SQLException e) {System. out. println ("getResultSetQuery error"); e. printStackTrace () ;}} return null;} // Add, modify, and delete the public void getResultSetUpdate (Statement statement, String SQL) {if (statement! = Null) {try again statement.exe cuteUpdate (SQL);} catch (SQLException e) {System. out. println ("getResultSetUpdate error"); e. printStackTrace () ;}}// close the Connection to public static void colse (Connection con) {if (con! = Null) {try {con. close ();} catch (SQLException e) {e. printStackTrace () ;}}// close the public static void close (Statement statement) {if (statement! = Null) {try {statement. close ();} catch (SQLException e) {e. printStackTrace () ;}}// close the query set public static void close (ResultSet set) {if (set! = Null) {try {set. close ();} catch (SQLException e) {e. printStackTrace () ;}}} test: [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 = 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 ();}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.