这个是链接数据库的 package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Dao {
protected static String dbClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
protected static String dbUrl = "jdbc:microsoft:sqlserver://127.0.0.1:1433;" + "DatabaseName=tsglxt;";
//127.0.0.1为IP地址,1433为接口;tsglxt是数据库名
protected static String dbUser = "sa";//登录名
protected static String dbPwd = "cfl780520";//登录密码
private static Connection conn = null;//定义一个静态数据库连接
/*
*
*/
private Dao() {// 获得数据库连接
try {
if (conn == null) {
Class.forName(dbClassName).newInstance();//装载数据库驱动
conn = DriverManager.getConnection(dbUrl,dbUser,dbPwd);
} else
return;
} catch (Exception e) {
e.printStackTrace();
}
}
static ResultSet executeQuery(String sql) {// 执行查询操作
try {
if (conn == null)
new Dao();
return conn.createStatement().executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
static int executeUpdate(String sql) {// 执行其他操作
try {
if (conn == null)
new Dao();
return conn.createStatement().executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
return -1;
}
}
public static void close() {// 关闭连接
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
}
这个是模糊查询,运行之后没有反应也不显示出错
//按出版社查询-模糊查询
public static List selectBookByPublish(String Publish) {
List list = new ArrayList();
String sql = "select ISBN,typeid,bookname,author,publish,publishdate,printtime,unitprice,typename from book join booktype on book.typeid=booktype.booktypeid and publish like '%'" +Publish + "'%'";感觉这里出了错
ResultSet rs = Dao.executeQuery(sql);
try {
while (rs.next()) {
Book Book = new Book();
Book.setISBN(rs.getString("ISBN"));
Book.setBooktypeid(rs.getInt("typeid"));
Book.setBookname(rs.getString("bookname"));
Book.setAuthor(rs.getString("author"));
Book.setPublish(rs.getString("publish"));
Book.setPublishdate(rs.getDate("publishdate"));
Book.setPrinttime(rs.getInt("printtime"));
Book.setUnitprice(rs.getDouble("unitprice"));
Book.setBooktypename(rs.getString("typename"));
list.add(Book);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
like '%" +Publish + "%'";
String sql = "select ISBN,typeid,bookname,author,publish,publishdate,printtime,unitprice,typename from book join booktype on book.typeid=booktype.booktypeidandlike_ '%" +Publish + "%'";_
我变斜的地方是你错误的地方(‘ ’这个是包涵整个模糊部分的,“ ”这个是你要搜索的数据依赖)
哪里来的这么多麻烦??????? 声明\不就好了
String sql = "select ISBN,typeid,bookname,author,publish,publishdate,printtime,unitprice,typename from book join booktype on book.typeid=booktype.booktypeid and publish like "+"%" +Publish +" %";
不对,让你来吻我
and publish like '%'" +Publish + "'%'";改为 where publish like "%" + Publish + "%"