?? tabledemo.java
字號(hào):
import java.sql.*;
public class TableDemo{
private String dbURL="jdbc:odbc:example"; //數(shù)據(jù)庫(kù)標(biāo)識(shí)名
private String user="devon"; //數(shù)據(jù)庫(kù)用戶
private String password="book"; //數(shù)據(jù)庫(kù)用戶密碼
public TableDemo(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //裝載數(shù)據(jù)庫(kù)驅(qū)動(dòng)
Connection con=DriverManager.getConnection(dbURL,user,password); //得到連接
Statement st=con.createStatement();
//新建表
System.out.println("新建表:products"); //輸出信息到控制臺(tái)
String sqlStr="create table products(Name varchar(20),Price float,Provider varchar(40),Count int)"; //新建表的SQL語(yǔ)句
st.executeUpdate(sqlStr); //執(zhí)行SQL語(yǔ)句,新建表
//插入數(shù)據(jù)
sqlStr="insert into products(Name,Price,Provider,Count) values(\'面包\',2.5,\'上海\',20)"; //插入數(shù)據(jù)SQL語(yǔ)句
st.executeUpdate(sqlStr); //執(zhí)行插入
sqlStr="insert into products(Name,Price,Provider,Count) values(\'蛋糕\',5.5,\'北京\',13)";
st.executeUpdate(sqlStr);
//顯示數(shù)據(jù)
sqlStr="select * from products"; //查詢數(shù)據(jù)SQL語(yǔ)句
ResultSet rs=st.executeQuery(sqlStr); //獲取結(jié)果集
String name,provider;
float price;
int count;
while (rs.next()){
name=rs.getString("Name"); //取得查詢結(jié)果
price=rs.getFloat("Price");
provider=rs.getString("Provider");
count=rs.getInt("Count");
System.out.println("Name:"+name+"\tPrice:"+price+"\tProvider:"+provider+"\tCount:"+count); //顯示查詢結(jié)果
}
//刪除表
System.out.println("刪除表:products");
sqlStr="drop table products"; //刪除表SQL語(yǔ)句
st.executeUpdate(sqlStr); //執(zhí)行刪除
con.close(); //關(guān)閉連接
}
catch (Exception ex) {
ex.printStackTrace(); //輸出出錯(cuò)信息
}
}
public static void main(String args[]){
new TableDemo();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -