您的位置:首页 > 博客中心 > 数据库 >

myeclipse8.5+tomcat6.0+sqlserver2008

时间:2022-03-14 04:45

尼玛,我忙活了半天,一直搜索是怎么回事,试过n便,包括下载多少版本的sqljdbc4包,不断的修改myeclipse的设置和sql server 2008。

最后原来是原来那个导入的SHB有问题,我新建一个web project,然后把SHB里面的东西,复制到新建的project就好了。

但是也还是有好处的,我再次温习了下Myeclipse tomcat sqlserver2008的连接

Tomcat就比较简单了,指定tomcat目录和jdk的目录

Sql server 2008

首先安装就比较麻烦,尤其是登录名

这次成功的是用电脑名\Administrator

配置

先修改端口,两处

技术分享

技术分享

然后导入sqljdbc4包。

示例代码JSP:

<%@ page language="java"import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>

<%

String path =request.getContextPath();

String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP ‘index.jsp‘ starting page</title>

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet"type="text/css" href="styles.css">

    -->

  </head>

 

  <body  bgcolor="c0ffc0">

  <%

    Stringcollege="政法 ";

    int i=0;

    Stringuser=""; 

    user =(String)session.getAttribute("username_session"); 

    if(user==null || user.equals("")){

        user="游客";

    }

    else

        ;

    //out.println(user);

   %>

      <table  align="center"  border=‘0‘>

         <tr><td><img width="800" height="200"src="source1/1.jpg"/> </td></tr>

      </table>

      <table  align="center"  border=‘1‘ bordercolor="c0ff00" bgcolor=""rules=rows>

        <tr>

            <td>您好<a href="modi_person_info.jsp"><%=user %></a>,欢迎光临!请</td>

            <td><table><tr><td><input type="button" value="登陆"  onclick="window.location.href=‘denglu.html‘"></td></tr></table></td>

            <td><table><tr><td><input type="button" onclick="window.location.href=‘zhuceqian.jsp‘" value="注册"/></td></tr></table></td>

            <td><input type="button" value="退出" name="logout" onclick="window.location.href=‘tuichu.jsp‘"/></td>

            <td><table><tr><td width="360"><input type="button"value="管理员登陆" onclick="window.location.href=‘admin_denglu.html‘"></td></tr></table></td>

            <td>

               <table align="right"><tr><td><input type="button"onclick="window.location.href=‘shopCar.jsp‘" value="我的购物车" id="shopCar"/></td></tr></table>

            </td>

        </tr>

    </table>

   <table width="800"  align="center"border=1 height="40"bordercolor="c0ff00" bgcolor="ffffff">

    <tr><td width="200"  align="center"><a href="index.jsp">首页</a></td><td width="200" align="center"><a href="er_shou_shu.jsp">二手书</a></td><td width="200"  align="center"><a href="liu_yan_ban.jsp">留言板</a></td><td width="200"align="center"><a href="lian_xi_wo_men.jsp">联系我们</a></td></tr>

   </table>

   

   <p></p>

   <table align="center"  >

   <tr><td align="center"><formname="serach_college_book" action="sousuoshuji.jsp?college=<%=college %>"method="get">

           <input type="text" name="college_book_search"id="college_book_text"/>

           &nbsp;&nbsp;<input type="submit" name="college_book_search"id="college_book_submit" value="搜索"/>

   </form></td></tr>

   <tr>

   <!--  <td><tablewidth="200"><trheight="50"><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;学院</td></tr><tr><tdheight="30">数学计算机学院</td></tr><tr><td height="30">化学与化工学院</td></tr><tr><tdheight="30">物理电气信息学院</td></tr><tr><td height="30">土木与水利工程学院</td></tr><tr><tdheight="30">政法学院</td></tr><tr><td height="30">人文学院</td></tr><tr><tdheight="30">经济管理学院</td></tr><tr><td height="30">资源环境学院</td></tr><tr><tdheight="30">生命科学学院</td></tr><tr><td height="30">教育学院</td></tr><tr><tdheight="30">机械学院</td></tr><tr><td height="30">农学院</td></tr></table></td>-->

   <td>

   <!--table开始显示图书-->

  

   <table>

                  <tr>

        <%

        //连接数据库

        StringJDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎

       String connectDB = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SHB";// 数据源

       try {

           Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类

       } catch (ClassNotFoundException e) {

           //e.printStackTrace();

      

           System.exit(0);

       }

   

 

       try {

           String user_con = "sa";

           String password_con = "1234567";

           Connection con = DriverManager.getConnection(connectDB,user_con, password_con);// 连接数据库对象

           Statement stmt = con.createStatement();// 创建SQL命令对象

           // 读取数据

           String sql="selectbook_picture,book_name,book_out_price,book_stock from college_info wherecollege=‘政法 ‘";

           ResultSet rs = stmt.executeQuery(sql);// 返回SQL语句查询结果集(集合)

           // 循环输出每一条记录

           while (rs.next()){ 

            Stringpicture=rs.getString("book_picture");

            String bookname=rs.getString("book_name");

           

           

            Stringprice=rs.getString("book_out_price");

            Stringno=rs.getString("book_stock");

            intno_int=Integer.parseInt(no);

            if(no_int<=0)

               no="缺货";

           

           

            %>

           

           

            <td><table  align="center"><tr ><td align="center" width="163"><img width="100" height="120"src="source/<%=picture %>.jpg"/></td></tr><tr width="100"><td align="center" width="100"><a href="detail.jsp?bookid=<%=picture %>"><%=bookname %></a></td></tr><tr><td align="center">价格:<%=price %></td></tr><tr><td align="center">剩余量:<%=no %></td></tr></table></td>

           

            <%

            i=i+1;

            if(i%5==0){

               out.println("</tr><tr>");

            }

            %>

       

        <%

           }

           // 关闭连接

           stmt.close();// 关闭命令对象连接

           con.close();// 关闭数据库连接

       } catch (SQLException e) {

           e.printStackTrace();

           out.println("数据库连接错误");

           System.exit(0);

       }

       //数据库连接完成

     %>

               </tr>

               </table>

  

   <!--table结束显示图书-->

   </td>

   </tr></table>       

  </body>

</html>

当然也有java:

import java.sql.*;

public class javaConSQL

{

 public static void main(String[] args)

 {

  String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎

  String connectDB="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Mydb";

  try

  {

   Class.forName(JDriver);//加载数据库引擎,返回给定字符串名的类

  }catch(ClassNotFoundException e)

  {

   //e.printStackTrace();

   System.out.println("加载数据库引擎失败");

   System.exit(0);

  }    

  System.out.println("数据库驱动成功");

 

  try

  {

   String user="sa";

   String password="12345";

   Connection con=DriverManager.getConnection(connectDB,user,password);//连接数据库对象

   System.out.println("连接数据库成功");

   Statement stmt=con.createStatement();//创建SQL命令对象

   /*

   //创建表

   System.out.println("开始创建表");

   String query="create table TABLE1(IDNCHAR(2),NAME NCHAR(10))";//创建表SQL语句

   stmt.executeUpdate(query);//执行SQL命令对象

   System.out.println("表创建成功");

     

   //输入数据

   System.out.println("开始插入数据");

   String a1="INSERT INTO TABLE1VALUES(‘1‘,‘旭哥‘)";//插入数据SQL语句

   String a2="INSERT INTO TABLE1VALUES(‘2‘,‘伟哥‘)";

   String a3="INSERT INTO TABLE1VALUES(‘3‘,‘张哥‘)";

   stmt.executeUpdate(a1);//执行SQL命令对象

   stmt.executeUpdate(a2);  

   stmt.executeUpdate(a3);

   System.out.println("插入数据成功");

   */

  

   //读取数据

   System.out.println("开始读取数据");

   ResultSet rs=stmt.executeQuery("SELECT * FROM admin");//返回SQL语句查询结果集(集合)

   //循环输出每一条记录

   while(rs.next())

   {

    //输出每个字段

    System.out.println(rs.getString("ID")+"\t"+rs.getString("NAME"));

   }

   System.out.println("读取完毕");

  

   //关闭连接

   stmt.close();//关闭命令对象连接

   con.close();//关闭数据库连接

  }

  catch(SQLException e)

  {

   e.printStackTrace();

   //System.out.println("数据库连接错误");

   System.exit(0);

  }

 }

}

 

热门排行

今日推荐

热门手游