search
HomeDatabaseMysql TutorialMyeclipse10.7连接SQLServer数据库技术

(1)学习数据库的时候最恼火的是不知道怎么连接,不想VS那样在WPF中我们直接使用函数即可调用数据库,但是在java中我们要使用JDBC技术, 因此我们的方法肯定要复杂一些!看了很多博客明文,试了很多方法还是不成功,最后看了《JavaWeb技术详解》恍然大悟,

(1)学习数据库的时候最恼火的是不知道怎么连接,不想VS那样在WPF中我们直接使用函数即可调用数据库,但是在java中我们要使用JDBC技术,

因此我们的方法肯定要复杂一些!看了很多博客明文,试了很多方法还是不成功,最后看了《JavaWeb技术详解》恍然大悟,特给大家分享一下!

(2)首先看一下我的项目机构:

\

其中带有标出来的就是需要准备的;lib目录下的为jar包网上下载的;ManageUsers为自己编写的测试程序!

(3)如果你已经准备好了lib目录下的四个jar包则开始配置:

点击项目右键--properies---Java Bulid Path -----Libraries ---Add JARs ---弹出的框框点击你的项目--找到lib目录 选中四个jar包确定----在ok!

\

(4)然后配置你的sql server ;

找到 sql server configurationmanager 找到1 乳沟看到TCP/IP为 “已禁用”则改为 我下边所示 启用即可!

\

(5)演示的代码ManageUsers(已配置好servlet的xml配置)

 

package com.lc.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ManageUsers extends HttpServlet{

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<h1 id="管理用户">管理用户</h1>");
		
		Connection con = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		
		try {
			
			// 1.加载驱动
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
			
			// 2.得到连接
			
			con=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=DBName","username","password");/*数据库名用户名密码我改成我的了*/
			
			//3.创建PreparedStatement
			ps = con.prepareStatement("select * from users");
			rs = ps.executeQuery();
			
			out.println("<table border=1 margin=auto>");
			out.println("<tr><td>id</td><td>用户名</td><td>email</td><td>grade</td></tr>");
			//循环显示所有用户信息
			while(rs.next())
			{    //注意我去的顺序是1 2 4 5 因为的在数据库中的第三个 为密码 没有取出 所以为 1 2 4 5  如果不注意这点会报错的!
				out.println("<tr><td>"+rs.getInt(1)+
						"</td><td>"+rs.getString(2)+
						"</td><td>"+rs.getString(4)+
						"</td><td>"+rs.getInt(5)+
						"</td><tr>");
			}
			out.println("</table>");
			//5.根据结果处理
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		finally
		{
			try {

				if (rs != null) {

					rs.close();
				}
				if (ps != null) {

					ps.close();
				}
				if (con != null) {

					con.close();
				}
			} catch (Exception ex) {

				ex.printStackTrace();
			}
		}

		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}

(6)运行如下:
\

(7)到此已经成功!如果你在这方面遇到问题的话 欢迎提问 共同学习!

另外 如果遇到连接数据库的问题 欢迎访问我整理的一些错误处理方法:http://blog.csdn.net/xlgen157387/article/details/39055085

最后 祝你成功!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)Apr 10, 2025 am 09:36 AM

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL: Essential Skills for DevelopersMySQL and SQL: Essential Skills for DevelopersApr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Describe MySQL asynchronous master-slave replication process.Describe MySQL asynchronous master-slave replication process.Apr 10, 2025 am 09:30 AM

MySQL asynchronous master-slave replication enables data synchronization through binlog, improving read performance and high availability. 1) The master server record changes to binlog; 2) The slave server reads binlog through I/O threads; 3) The server SQL thread applies binlog to synchronize data.

MySQL: Simple Concepts for Easy LearningMySQL: Simple Concepts for Easy LearningApr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL: A User-Friendly Introduction to DatabasesMySQL: A User-Friendly Introduction to DatabasesApr 10, 2025 am 09:27 AM

The installation and basic operations of MySQL include: 1. Download and install MySQL, set the root user password; 2. Use SQL commands to create databases and tables, such as CREATEDATABASE and CREATETABLE; 3. Execute CRUD operations, use INSERT, SELECT, UPDATE, DELETE commands; 4. Create indexes and stored procedures to optimize performance and implement complex logic. With these steps, you can build and manage MySQL databases from scratch.

How does the InnoDB Buffer Pool work and why is it crucial for performance?How does the InnoDB Buffer Pool work and why is it crucial for performance?Apr 09, 2025 am 12:12 AM

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

MySQL: The Ease of Data Management for BeginnersMySQL: The Ease of Data Management for BeginnersApr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

When might a full table scan be faster than using an index in MySQL?When might a full table scan be faster than using an index in MySQL?Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment