1. 在jdbc中有3種執(zhí)行sql的語句分別是execute禁悠,executeQuery和executeUpdate
1.execute執(zhí)行增刪改查操作
execute返回的結(jié)果是個(gè)boolean型免钻,當(dāng)返回的是true的時(shí)候粱檀,表明有ResultSet結(jié)果集树枫,通常是執(zhí)行了select操作戚啥,當(dāng)返回的是false時(shí)袒啼,通常是執(zhí)行了insert年叮、update具被、delete等操作。execute通常用于執(zhí)行不明確的sql語句只损。
-
executeQuery執(zhí)行查詢操作
executeQuery返回的是ResultSet結(jié)果集一姿,通常是執(zhí)行了select操作。
executeUpdate執(zhí)行增刪改操作
executeUpdate返回的是int型跃惫,表明受影響的行數(shù)叮叹,通常是執(zhí)行了insert、update辈挂、delete等操作衬横。
2. navicat自增報(bào)錯(cuò)
先把外鍵刪了,再自增
3. 帶參數(shù)的java數(shù)據(jù)庫增刪查改
public int save(User user) throws SQLException {
String sql = "insert into user values(0,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getTele());
pstmt.setDate(3, user.getBirthday());
int n = pstmt.executeUpdate();
pstmt.close();
return n;
}
public int delete(User user) throws SQLException{
String sql = "delete from user where id = "+user.getId();
Statement stmt = conn.createStatement();
int n = stmt.executeUpdate(sql);
stmt.close();
return n;
}
public int update(User user) throws SQLException{
String sql = "update user set name=?, tele=?, birthday=? where id = "+user.getId();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(2, user.getName());
pstmt.setString(3, user.getTele());
pstmt.setDate(4, user.getBirthday());
int n = pstmt.executeUpdate(sql);
pstmt.close();
return n;
}
public User getUser(Integer id) throws SQLException{
String sql = "select * from user where id = " + id;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
User user = getUserFromResultSet(rs);
rs.close();
stmt.close();
return user;
}
4.java.sql.SQLException: Before start of result set異常及處理辦法
解決方法:使用rs.getString();前一定要加上rs.next();
原因:ResultSet對(duì)象代表SQL語句執(zhí)行的結(jié)果集终蒂,維護(hù)指向其當(dāng)前數(shù)據(jù)行的光標(biāo)蜂林。每調(diào)用一次next()方法遥诉,光標(biāo)向下移動(dòng)一行。最初它位于第一行之前噪叙,因此第一次調(diào)用next()應(yīng)把光標(biāo)置于第一行上矮锈,使它成為當(dāng)前行。隨著每次調(diào)用next()將導(dǎo)致光標(biāo)向下移動(dòng)一行睁蕾。在ResultSe對(duì)象及其t父輩Statement對(duì)象關(guān)閉之前苞笨,光標(biāo)一直保持有效。
5. java窗口跳轉(zhuǎn)不了
解決子眶;
6. Unknown column '' in 'field list'報(bào)錯(cuò)及解決
將代碼修改為使用 pstmt.set瀑凝?傳參給sql語句
7. 實(shí)現(xiàn)jtable更新
使用使用TableModel的方式存放Table需要顯示的數(shù)據(jù)
TableModel 繼承AbstractTableModel ,進(jìn)而實(shí)現(xiàn)了接口TableModel
在TableModel 中提供一個(gè)table顯示需要的所有信息
- getRowCount 返回一共有多少行
- getColumnCount 返回一共有多少列
- getColumnName 每一列的名字
- isCellEditable 單元格是否可以修改
- getValueAt 每一個(gè)單元格里的值
我的tablemodel
public class MbussinessG extends AbstractTableModel{
private static Integer bid = 0;
static List<goods> g ;
private static myutil dbUtil = new myutil();
String[] columnNames = new String[] { "Name", "Detatil", "Image" ,"price"};
public MbussinessG(bussiness myBussiness) throws Exception{
bid = myBussiness.getID();//獲取商家ID
g = showallgoods(bid);
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return g.size();
}
@Override
public String getColumnName(int columnIndex) {
// TODO Auto-generated method stub
return columnNames[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
goods result = g.get(rowIndex);
if (columnIndex == 0)
return result.getGoodsName();
else if (columnIndex == 1)
return result.getGoodsDetail();
else if (columnIndex == 2)
return result.getGoodsImg();
else if (columnIndex == 3)
return result.getPrice();
return null;
}
public static void main(String[] args, bussiness myBussiness) throws Exception {
new MbussinessG(myBussiness);
}
//獲取發(fā)布的所有商品
public static List<goods> showallgoods(Integer bid) throws Exception {
String sql = null;
sql = "select * from products where status='1' and b_id=?";//默認(rèn)status設(shè)為1臭杰,為上線
Connection con = dbUtil.getCon();
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, bid);
ResultSet rs=pstmt.executeQuery(); //將sql語句傳至數(shù)據(jù)庫粤咪,返回的值為一個(gè)字符集用一個(gè)變量接收
List<goods> result = new ArrayList<goods>();
while(rs.next()){ //next()獲取里面的內(nèi)容
Integer gid = rs.getInt("p_id");
String gname =rs.getString("p_name");
String gdetail = rs.getString("detail");
String gimage = rs.getString("main_image");
float gprice = rs.getFloat("price");
result.add(new goods(gname, gdetail,gimage, gprice));
}
return result;
}
}
使用tablemodel(關(guān)鍵部分代碼)
//我的商品jp3
MbussinessG htm = new MbussinessG(myBussiness);
//根據(jù) TableModel來創(chuàng)建 Table
JTable t = new JTable(htm);
JScrollPane jp3 = new JScrollPane(t);
tabbedPane.addTab("我的商品",jp3);
需要更新的時(shí)候,比如我是發(fā)布商品后需要更新我的商品table
MbussinessG.g = showallgoods(bid);//更新tablemodel的數(shù)據(jù)
t.updateUI();//更新ui
8.sql語句報(bào)錯(cuò)
sql語句沒錯(cuò)渴杆,發(fā)現(xiàn)是表名取了order是數(shù)據(jù)庫關(guān)鍵字所以報(bào)錯(cuò)寥枝!摔杯子!
9.sql語句磁奖,兩個(gè)表連接錯(cuò)誤
兩個(gè)表連接的公共屬性囊拜,必須是其中一個(gè)表的主鍵:)