點(diǎn)擊電腦的開(kāi)始。
我們一直強(qiáng)調(diào)網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)對(duì)于企業(yè)的重要性,如果您也覺(jué)得重要,那么就需要我們慎重對(duì)待,選擇一個(gè)安全靠譜的網(wǎng)站建設(shè)公司,企業(yè)網(wǎng)站我們建議是要么不做,要么就做好,讓網(wǎng)站能真正成為企業(yè)發(fā)展過(guò)程中的有力推手。專業(yè)的建站公司不一定是大公司,創(chuàng)新互聯(lián)建站作為專業(yè)的網(wǎng)絡(luò)公司選擇我們就是放心。
首先點(diǎn)擊電腦的開(kāi)始,找到QLServer配置管理器,將SQLServer網(wǎng)絡(luò)配置中的SQLEXPRESS的協(xié)議如下圖中的三個(gè)服務(wù)改為啟用,然后點(diǎn)擊TCPIP協(xié)議,修改動(dòng)態(tài)端口為1433,重啟服務(wù)器才能生效,首先建立一個(gè)java項(xiàng)目,命名為T(mén)estDB然后右鍵點(diǎn)擊TestDB選擇buildpath然后找到AddExternalJARs,找到下載的sqljdbc的包,打開(kāi)即可,然后項(xiàng)目的referencedlibraries目錄下能看到導(dǎo)入的包,新建一個(gè)包database包中再建立TestConect執(zhí)行以下代碼,代碼執(zhí)行結(jié)果和數(shù)據(jù)庫(kù)中查詢的結(jié)果一樣。
%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312" /
title無(wú)標(biāo)題文檔/title
head
body bgcolor="cyan"
center
h1 align="center" class="STYLE1"請(qǐng)輸入要查詢的關(guān)鍵字/h1
form action="jie.jsp" method="post"
span class="STYLE2"請(qǐng)選擇查詢條件/span
select name="condition"
option values ="author"作者/option
option values ="name" selected="selected"書(shū)名/option
/select
span class="STYLE3"請(qǐng)輸入/span
input type="text" name="name" /br /
input type="submit" value="提交" /
/form
/body
/html
跳轉(zhuǎn)后的頁(yè)面:
%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %
html
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312" /
title無(wú)標(biāo)題文檔/title
head
%@ page import="test.BookCo" %
%@ page import="java.sql.*" %
body
jsp:useBean id="Co" class="test.BookCo" scope="application"
/jsp:useBean
center
h1圖書(shū)信息/h1
table border="1" width="80%"
th書(shū)號(hào)/thth書(shū)名/thth作者/thth出版社/thth書(shū)籍簡(jiǎn)介/th
%
String name=request.getParameter("name");
String condition=request.getParameter("condition");
Co.setKeyword(name,condition);
ResultSet rs=Co.Boo();
while(rs.next())
{
out.print("trtd"+rs.getInt("bId")+"/td");
out.print("td"+rs.getString("bName")+"/td");
out.print("td"+rs.getString("bAuthor")+"/td");
out.print("td"+rs.getString("bPress")+"/td");
out.print("td"+rs.getString("bInfo")+"/td/tr");
}
rs.close();
%
/table
/center
/body
/html
javabean部分:
package test;
import java.sql.*;
import java.io.*;
import java.util.*;
public class BookCo{
public String name,condition;
Connection con=null;
String strurl="jdbc:odbc:bookstore";
Statement stmt=null;
ResultSet rs=null;
public BookCo()
{
name="";
condition="";
}
public void setKeyword(String s,String ss)
{
name=s;
condition=ss;
try{
byte[] b=name.getBytes("iso-8859-1");
name=new String(b);
byte[] bb=condition.getBytes("iso-8859-1");
condition=new String(bb);
}
catch(Exception e){System.out.println("error1:"+e.getMessage());}
}
public ResultSet Boo()
{
String drivername="sun.jdbc.odbc.JdbcOdbcDriver";
String sql="select * from books";
try {
Class.forName(drivername);
con=DriverManager.getConnection(strurl);
stmt=con.createStatement();
if(condition.equals("作者")){
sql="select * from books where bAuthor='"+name+"'";
}
else
if(condition.equals("書(shū)名"))
{
sql="select * from books where bName='"+name+"'";
}
rs=stmt.executeQuery(sql);
}
catch (Exception e){
System.out.println("connection fail!");
}
return rs;
}}
sql語(yǔ)句么?
首先我得問(wèn)一下數(shù)據(jù)庫(kù)中圖書(shū)信息表你怎么建的 有哪些字段 如果有圖書(shū)數(shù)量這個(gè)字段 可以直接用下面的sql語(yǔ)句
select * from (表名) where (圖書(shū)數(shù)量字段)5
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileReader;
import?java.util.ArrayList;
import?java.util.List;
import?java.util.Scanner;
public?class?BookWork?{
static?ListBook?data;
private?static?Scanner?input;
public?static?void?main(String[]?args)?{
if?(!initBook("d:/book.txt"))?{
System.out.println("初始圖書(shū)列表失敗?..");
return;
}
input?=?new?Scanner(System.in);
while?(true)?{
try?{
System.out.println("請(qǐng)輸入操作:");
System.out.println("1.找書(shū)??2.刪除圖書(shū)?3.退出");
int?number?=?Integer.parseInt(input.next());
if?(number?==?1)?{
findBook();
}?else?if?(number?==?2)?{
delBook();
}?else?if?(number?==?3)?{
System.out.println("退出");
break;
}?else?{
System.out.println("這個(gè)不是我要的...重來(lái)...");
System.out.println();
}
}?catch?(Exception?e)?{
e.printStackTrace();
System.out.println("這個(gè)不是我要的...重來(lái)...");
System.out.println();
}
}
}
private?static?void?delBook()?{
System.out.println("請(qǐng)輸入要?jiǎng)h除的書(shū)名或編號(hào):");
String?key?=?input.next();
if?(key?!=?null??!key.equals(""))?{
for?(Book?book?:?data)?{
if?(book.number.equals(key)?||?book.name.contains(key))?{
data.remove(book);
System.out.println("?圖書(shū)?"?+?book.toString()?+?"?已刪除");
return;
}
}
}
System.out.println("沒(méi)有您要?jiǎng)h除的");
}
private?static?void?findBook()?{
System.out.println("請(qǐng)輸入要查找的書(shū)名或編號(hào):");
String?key?=?input.next();
if?(key?!=?null??!key.equals(""))?{
for?(Book?book?:?data)?{
if?(book.number.equals(key)?||?book.name.contains(key))?{
System.out.println("找到了?圖書(shū)?"?+?book.toString());
return;
}
}
}
System.out.println("沒(méi)有您要找的");
}
private?static?boolean?initBook(String?string)?{
try?{
System.out.println("圖書(shū)導(dǎo)入中...");
System.out.println("列表文件?--?"?+?string);
File?file?=?new?File(string);
if?(!file.exists())?{
return?false;
}
data?=?new?ArrayListBook();
BufferedReader?bufferedReader?=?new?BufferedReader(new?FileReader(file));
String?line?=?"";
while?((line?=?bufferedReader.readLine())?!=?null)?{
String[]?strings?=?line.split(",");
Book?b?=?new?Book(strings[0],?strings[1]);
data.add(b);
System.out.println("導(dǎo)入"?+?b.toString());
}
}?catch?(Exception?e)?{
e.printStackTrace();
return?false;
}
return?true;
}
public?static?class?Book?{
String?number;
String?name;
public?Book(String?number,?String?name)?{
super();
this.number?=?number;
this.name?=?name;
}
@Override
public?String?toString()?{
return?"Book?[編碼:"?+?number?+?",?名稱:"?+?name?+?"]";
}
}
}
001,金瓶梅
002,雜事秘辛
003,飛燕外傳
004,控鶴監(jiān)秘記
005,漢宮春色