环境sqlserver2005和myeclipse6.0
1.加载驱动的JSP页面代码如下:
<%@ page language="java" import="java.util.*,java.sql.*,java.lang.*" pageEncoding="gb2312"%>
//需要的包需要IMPORT进来,java.sql.Statement,
//java.sql.Connection,java.sql.ResultSet,java.DriverManager等。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My sqlserver2005 starting page </title>
</head>
<body>
This is my JSP page. <br>
<%
Connection conn=null;
String user="sa";
String pwd="123";
String url="jdbc:sqlserver://127.0.0.1:1433;databaseName=test";
try{
//加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex)
{//驱动是否异常
ex.printStackTrace();
}
try{
//定义执行的操作
String sql="select * from stuInfo";
//建立链接
conn=DriverManager.getConnection(url,user,pwd);
//创建statement对象与sql发生消息
Statement stm=conn.createStatement();
//创建resultset对象返回处理结果
ResultSet rs=stm.executeQuery(sql);
System.out.println("This is sa.");
while(rs.next())
{
//输出1,3,2,列的值,测试是否连通可用。
System.out.println(rs.getString(1)+" "+rs.getString(3)+""+rs.getString(2));
}
}
catch(SQLException e)
{
e.printStackTrace();
}
//判断下是否有链接异常
if(conn==null)
System.out.print("error");
else
System.out.println("ok");
System.out.println("I can see the result!");
%>
</body>
</html>
//注:jar包需要导入到软件,否则可能以为找不到导致conn=null,而加载失败。
6-1
批量添加创建表stuInfo
drop table stuInfo;
create table stuInfo
(
stuNo char(5),
stuName varchar(10),
stuSex char(2)
);
begin
declare @s int;
declare @i int;
set @s=1;
set @i=1001;
while (@s<=10)
begin
if (@s=2 or @s=4 or @s=5 or @s=8 or @s=10)
insert into stuInfo values(@s,'lily'+convert(varchar(5),@s),'男');
else
insert into stuInfo values(@s,'lily'+convert(varchar(5),@s),'女');
set @s=@s+1;
set @i=@i+1;
end;
end;
select * from stuInfo
6-2