In Java EE development, we must use the database, then in the development of Java EE, how to use code implementation and SQL2008 connection? In this article, I'll walk you through the simplest way to connect a SQL2008 database using JDBC.
First, let's look at some information about our database.
User name: SA
Password: 123456
Database name: Servletuser
Then let's take a quick look at the project directory
Note: The connection database needs to be imported into the external package, because my JDK is 1.7, so here I am importing Sqljdbc4.jar, this external package can be downloaded on Microsoft's website
JDBC External package: http://www.microsoft.com/zh-cn/download/details.aspx?id=21599
After the download is complete, unzip the compressed file, and then we can use the package inside.
Put Sqljdbc4.jar (my JDK is 1.7, so import this, other JDK depends on the situation) into the project.
At this point, our preparations are done, and now the code is written.
[Java]View Plaincopyprint?
- Package Demo;
- Import java.sql.Connection;
- Import Java.sql.DriverManager;
- Import java.sql.SQLException;
- Public class Jdbctext {
- Public static void Main (string[] args) {
- Connection dbconn = null;
- String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; //load JDBC driver
- Connecting servers and Databases Servletuser
- String Dburl = "jdbc:sqlserver://localhost:1433; Databasename=servletuser ";
- String userName = "sa"; //Default user name
- String userpwd = "123456"; //Password
- try {
- Class.forName (drivername);
- Dbconn = Drivermanager.getconnection (Dburl, UserName, userpwd);
- System.out.println ("Connection successful!"); //If the connection is successful
- } catch (Exception e) {
- E.printstacktrace ();
- }finally{
- To release the resources
- if (dbconn!=null) {
- try {
- Dbconn.close ();
- } catch (SQLException e) {
- E.printstacktrace ();
- }
- }
- }
- }
- }
Package Demo;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.sqlexception;public class Jdbctext {public static void main (string[] args) {Connection dbconn = null; String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; Load JDBC driver//connection server and database servletuserstring dburl = "jdbc:sqlserver://localhost:1433; Databasename=servletuser "; String userName = "sa"; Default User name string userpwd = "123456"; Password try {class.forname (drivername);d bconn = Drivermanager.getconnection (Dburl, UserName, userpwd); System.out.println ("Connection successful!"); If the connection succeeds} catch (Exception e) {e.printstacktrace ();} finally{//the release of the resource if (dbconn!=null) {try {dbconn.close ();} catch (SQLException e) {e.printstacktrace ();}}}}
The code is relatively simple, we should be able to see the easy to understand, but I did in the first process, there was a mistake
TCP/IP connection to host localhost via port 1433 failed.
This problem occurs because our SQL database meter has a TCP/IP connection enabled and we need to open the TCO/IP connection in SQL Sevice configeration Manager
After the connection is opened, we need to restart the SQL service for the configuration to work
Then start the application again and it should be successful.
Http://www.cnblogs.com/oversea201405/p/3749549.html
How to use JDBC to connect Sql2008 databases in Java (GO)