数据库
关于SQLSERVER语句?
一、关于SQLSERVER语句?
sqlserver语句添加列步骤如下:
1、首先我们准备一个数据表,接下来将会在这个表中添加列。
2、然后我们通过alter table语句来给表添加一个列。
3、回到数据表一会我们看到列已经被添加进数据表了。
4、接下来我们在添加列的时候同时添加上默认值,这个时候运用default关键字。
5、然后回到数据表,我们就看到默认值有内容了。
6、另外在数据表中经常使用的是uniqueidentifier类型,这种字段设置默认值。
7、回到数据表中我们可以看到默认值已经添加上了。
二、SQLServer数据库更新语句突然变得很慢?
将有数据的表备份出来,恢复在另外一台机器,而后把要复制到新数据库的数据导出来后,用bulk insert或excel的方式导入到新的数据库。
三、sqlserver数据库if-else条件判断语句怎么使用?
1、首先写if语句之前都要声明一个变量,用来进行条件判断,如下图所示。
2、然后根据变量的值进行if分支结构的编写,如下图所示。
3、运行一下就可以看到if生效了,如下图所示。
4、另外也可以在if后面加else语句,和其他的语言是一样的,如下图所示。
5、运行程序以后,就走到了else里面了,如下图所示。
6、最后if语句里面是可以进行if嵌套的,如下图所示。
四、J2EE中如何使用SQLSERVER数据库的语句?
经过归纳得出J2EE sqlserver 连接数据库的三种配置:
一、连接池方式:
1、连接迟3个包+sqlserver驱动包复制到tomcat/common/lib
2、配置tomcat/conf/context.xml,注意2000和2005 驱动名字和路径
[xhtml] view plaincopyprint?
<Resource name="jdbc/pubs"
auth="Container" type="javax.sql.DataSource" maxActive="100"
maxIdle="30" maxWait="10000" username="sa" password="120010"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=books"/>
[xhtml] view plaincopyprint?
<Resource name="jdbc/pubs"
auth="Container" type="javax.sql.DataSource" maxActive="100"
maxIdle="30" maxWait="10000" username="sa" password="120010"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=books"/>
[java] view plaincopyprint?
//2000:
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=books"/>
//2005:
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=books"/>
[java] view plaincopyprint?
//2000:
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=books"/>
//2005:
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=books"/>
3、在工程web.xml添加节点
[c-sharp] view plaincopyprint?
<resource-ref>
<res-ref-name>jdbc/pubs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
[c-sharp] view plaincopyprint?
<resource-ref>
<res-ref-name>jdbc/pubs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4、得到连接的方法内导如以下几个包:
[c-sharp] view plaincopyprint?
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
// 得到Connection对象的方法
public static Connection getConnection(){
try {
Context ic = new InitialContext();
DataSource source = (DataSource)ic.lookup
("java:comp/env/jdbc/bbs");
con = source.getConnection();
}catch(NamingException ex){
ex.printStackTrace();
}catch(SQLException ex){
ex.printStackTrace();
}
return con;
}
[c-sharp] view plaincopyprint?
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
// 得到Connection对象的方法
public static Connection getConnection(){
try {
Context ic = new InitialContext();
DataSource source = (DataSource)ic.lookup
("java:comp/env/jdbc/bbs");
con = source.getConnection();
}catch(NamingException ex){
ex.printStackTrace();
}catch(SQLException ex){
ex.printStackTrace();
}
return con;
}
================================================================================
二、读取属性文件方式
[java] view plaincopyprint?
*.properties文件
driverName=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;DatabaseName=books
user=sa
password=123
//读取*.properties文件的类
import java.io.InputStream;
import java.util.Properties;
public final class Env extends Properties {
private static Env instance;
public static Env getInstance(){
if(instance != null){
return instance;
}else{
makeInstance();
return instance;
}
}
//synchronized 同步方法,保证同一时间只能被一个用户调用
private static synchronized void makeInstance(){
if(instance == null){
instance = new Env();
}
}
private Env(){
InputStream is =getClass().getResourceAsStream("db.properties");//配置文件位置
try{
load(is);
}catch(Exception ex){
System.err.println("请确认读取的文件是否存在!");
}
}
//测试连接是否成功方法方法
public static void main(String[] args) {
System.out.println(getInstance().getProperty("driverName"));
}
}
[java] view plaincopyprint?
*.properties文件
driverName=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;DatabaseName=books
user=sa
password=123
//读取*.properties文件的类
import java.io.InputStream;
import java.util.Properties;
public final class Env extends Properties {
private static Env instance;
public static Env getInstance(){
if(instance != null){
return instance;
}else{
makeInstance();
return instance;
}
}
//synchronized 同步方法,保证同一时间只能被一个用户调用
private static synchronized void makeInstance(){
if(instance == null){
instance = new Env();
}
}
private Env(){
InputStream is =getClass().getResourceAsStream("db.properties");//配置文件位置
try{
load(is);
}catch(Exception ex){
System.err.println("请确认读取的文件是否存在!");
}
}
//测试连接是否成功方法方法
public static void main(String[] args) {
System.out.println(getInstance().getProperty("driverName"));
}
}
注意类的调用:譬如String url = Env.getInstance().getProperties("url");就能得到相应
的字符窜
驱动driverName,用户user, 密码password获取方式同上
==========================================================
三、读取xml文件中的节点
首先报连接池包3个和1个数据库驱动包复制到工程下WEB-INF/lib中
1、工程下的web.xml要添加以下节点:
[xhtml] view plaincopyprint?
<context-param>
<param-name>driverName</param-name>
<param-value>com.microsoft.sqlserver.jdbc.SQLServerDriver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:sqlserver://localhost:1433;DatabaseName=bbs</param-value>
</context-param>
<context-param>
<param-name>userName</param-name>
<param-value>sa</param-value>
</context-param>
<context-param>
<param-name>passWord</param-name>
<param-value>123</param-value>
</context-param>
[xhtml] view plaincopyprint?
<context-param>
<param-name>driverName</param-name>
<param-value>com.microsoft.sqlserver.jdbc.SQLServerDriver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:sqlserver://localhost:1433;DatabaseName=bbs</param-value>
</context-param>
<context-param>
<param-name>userName</param-name>
<param-value>sa</param-value>
</context-param>
<context-param>
<param-name>passWord</param-name>
<param-value>123</param-value>
</context-param>
2、新建一个普通类继承HttpServlet类,并实现ServletContextListener监听接口,如下:
[c-sharp] view plaincopyprint?
import java.sql.Connection;
import java.sql.ResultSet;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;
public class ContextListener extends HttpServlet implements ServletContextListener {
/**
* 销毁servlet
*/
public void contextDestroyed(ServletContextEvent sc) {
}
/**
* 初始化
*/
public void contextInitialized(ServletContextEvent sc) {
System.out.println("开启服务:");
ServletContext servletContext = sc.getServletContext();
String driverName = servletContext.getInitParamete("driverName");
String url = servletContext.getInitParameter("url");
String userName = servletContext.getInitParameter("userName");
String passWord = servletContext.getInitParameter("passWord");
BaseDAO.setDriverName(driverName);
BaseDAO.setUrl(url);
BaseDAO.setUser(userName);
BaseDAO.setPassword(passWord);
}
}
[c-sharp] view plaincopyprint?
import java.sql.Connection;
import java.sql.ResultSet;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;
public class ContextListener extends HttpServlet implements ServletContextListener {
/**
* 销毁servlet
*/
public void contextDestroyed(ServletContextEvent sc) {
}
/**
* 初始化
*/
public void contextInitialized(ServletContextEvent sc) {
System.out.println("开启服务:");
ServletContext servletContext = sc.getServletContext();
String driverName = servletContext.getInitParamete("driverName");
String url = servletContext.getInitParameter("url");
String userName = servletContext.getInitParameter("userName");
String passWord = servletContext.getInitParameter("passWord");
BaseDAO.setDriverName(driverName);
BaseDAO.setUrl(url);
BaseDAO.setUser(userName);
BaseDAO.setPassword(passWord);
}
}
3、在公共类BaseDao中
[java] view plaincopyprint?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource; //连接池要到包
/*
* 获取数据库连接
*/
public class BaseDAO {
private static Connection con;
private static String driverName;
private static String url;
private static String userName;
private static String passWord;
//获取连接对象Connection
public static Connection getConnection(){
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverName);
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(passWord);
try{
con = dataSource.getConnection();
} catch(SQLException ex) {
ex.printStackTrace();
}
return con;
}
/*
* 配置:从web.xml
*/
//驱动名称
public static String getDriverName() {
return getDriverName();
}
public static void setDriverName(String driverName) {
BaseDAO.driverName = driverName;
}
//URL
public static String getUrl(){
return getUrl();
}
public static void setUrl(String url) {
BaseDAO.url = url;
}
//用户名
public static String getUser(){
return getUser();
}
public static void setUser(String userName) {
BaseDAO.userName = userName;
}
//密码
public static String getPassWord(){
return getPassWord();
}
public static void setPassword(String passWord) {
BaseDAO.passWord = passWord;
}
}
[java] view plaincopyprint?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource; //连接池要到包
/*
* 获取数据库连接
*/
public class BaseDAO {
private static Connection con;
private static String driverName;
private static String url;
private static String userName;
private static String passWord;
//获取连接对象Connection
public static Connection getConnection(){
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverName);
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(passWord);
try{
con = dataSource.getConnection();
} catch(SQLException ex) {
ex.printStackTrace();
}
return con;
}
/*
* 配置:从web.xml
*/
//驱动名称
public static String getDriverName() {
return getDriverName();
}
public static void setDriverName(String driverName) {
BaseDAO.driverName = driverName;
}
//URL
public static String getUrl(){
return getUrl();
}
public static void setUrl(String url) {
BaseDAO.url = url;
}
//用户名
public static String getUser(){
return getUser();
}
public static void setUser(String userName) {
BaseDAO.userName = userName;
}
//密码
public static String getPassWord(){
return getPassWord();
}
public static void setPassword(String passWord) {
BaseDAO.passWord = passWord;
}
}
五、SQLSERVER2008查询语句不能执行也不能选择数据库?
你是直接打开这个SQLQuery2.sql这个文件的,说明你没登陆,空白地方右键登陆就行了。
六、SQLSERVER分组求和sql语句?
select sum(id4),count(*) from a_temp ;
可以一句SQL就直接查询得到列的和以及记录数。
该SQL中sum(id4)是列id4的总和,
count(*)是得到的数据总行数。
七、sqlserver语句怎么算质数?
根据质数的定义,对于一个自然数 N,我们可以检查它是否能够被任何从 2 到 N 的平方根加 1 的自然数整除。如果可以被整除,就不是质数;否则是质数。因此,可以使用以下查询返回 100 以内的所有质数:
WITH RECURSIVE t(n) AS (
SELECT 2
UNION ALL
SELECT n+1 FROM t WHERE n<100
)
SELECT t1.n
FROM t t1
WHERE t1.n = 2
OR NOT EXISTS (
SELECT 1
FROM t t2
WHERE t2.n BETWEEN 2 AND sqrt(t1.n)+1
AND t1.n%t2.n = 0
);
八、Android连接SQLServer数据库?
android好像没办法连接sqlserver吧。呵呵,如果你真想获取sqlserver中的数据,只能通过访问一个网页,以读取xml文件的方式来读取。
九、SQLSERVER数据库好学不?
不管是SQL SERVER还是ORACLE,或者DB2,MYSQL,学习数据库都那样,如果只想运用,建表,查询之类的,学习学习sql语句就行了,并不难。如果你要调优维护数据库上升到dba,那就有难度了。你想想人家月薪几万的dba,肯定不是白拿的啊。。
十、什么是SQLserver数据库?
美国Microsoft公司推出的一种关系型数据库系统。SQL Server是一个可扩展的、高性能的、为分布式客户机/服务器计算所设计的数据库管理系统,实现了与WindowsNT的有机结合,提供了基于事务的企业级信息管理系统方案。
热点信息
-
在Python中,要查看函数的用法,可以使用以下方法: 1. 使用内置函数help():在Python交互式环境中,可以直接输入help(函数名)来获取函数的帮助文档。例如,...
-
一、java 连接数据库 在当今信息时代,Java 是一种广泛应用的编程语言,尤其在与数据库进行交互的过程中发挥着重要作用。无论是在企业级应用开发还是...
-
一、idea连接mysql数据库 php connect_error) { die("连接失败: " . $conn->connect_error);}echo "成功连接到MySQL数据库!";// 关闭连接$conn->close();?> 二、idea连接mysql数据库连...
-
要在Python中安装modbus-tk库,您可以按照以下步骤进行操作: 1. 确保您已经安装了Python解释器。您可以从Python官方网站(https://www.python.org)下载和安装最新版本...