來來來,了解一下java的最基本的功能,連接數據庫進行增刪改查
怎么連接數據庫呢?
1.要連接數據庫受限得有支持鏈接數據庫的jar包,放置的位置如下,此處是鏈接mysql的jar:
Paste_Image.png
注:如果想連接其他數據庫,例如oracle可自行下載他的jar包然后放在同樣的地方即可
2.代碼展示:
'public static Connection getConn() throws ClassNotFoundException,SQLException {
try{
Connection con = null;
// 加載驅動程序
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1/test";//test是數據庫名稱
String user = "root";//賬號
String pwd = "1234qwer";//密碼
// 建立連接
con = DriverManager.getConnection(url, user, pwd);
catch(Exception e){
e.printStackTrace();
}
finally{
con.close();
stm.close();
rs.close();
}
}'
那怎么判斷我這個數據庫到底鏈接上了?
簡單的方法,如下:
try {
Connection con = ConnectionUtil.getConn();
System.out.println(con.isClosed());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
'
結果展示為1,證明鏈接失敗;結果展示為0,證明鏈接成功
向數據庫添加表格
'String sql ="create table Depart(depNo int primary key,depName varchar(50) not null,depMan varchar(50) not null) ";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 創建sql執行對象
int rs = st.executeUpdate(sql);//執行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
表格中插入數據
'String sql ="insert into Depart values(1,'ceshiyixia','woman')";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 創建sql執行對象
int rs = st.executeUpdate(sql);//執行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
表格中查詢數據
'String sql ="select * from Depart";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 創建sql執行對象
ResultSet rs = st.executeQuery(sql);//執行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
表格中更改字段
'String sql ="alter table Depart rename column depName to depName1";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 創建sql執行對象
int rs = st.executeUpdate(sql);//執行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
表格中修改數據
'String sql ="update Depart set depNo=20171020";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 創建sql執行對象
int rs = st.executeUpdate(sql);//執行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
表格中刪除數據
'String sql ="delete from Depart where depNo=20171020 ";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 創建sql執行對象
int rs = st.executeUpdate(sql);//執行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
刪除表格
'String sql ="drop table Depart ";
Connection con;
int i = 0;
try {
con = ConnectionUtil.getConn();
Statement st = (Statement) con.createStatement();// 創建sql執行對象
int rs = st.executeUpdate(sql);//執行sql
System.out.println(rs);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}'
刪除數據庫
想要知道代碼,請聽下回分解~~~其實就是sql語句的不同,看了這么多例子發現是不是都一樣呀,希望對入門的你有所幫助,謝謝觀看,后續會努力增加更精彩的內容!