對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪改查?
成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)洪湖,10年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢(xún)建站服務(wù):18982081108
以下是 sql server 2013+java.實(shí)現(xiàn)的是對(duì)MSC對(duì)象的增刪改查.
需要下載連接驅(qū)動(dòng)程序:com.microsoft.sqlserver.jdbc.SQLServerDriver
網(wǎng)上搜一下就行
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
class MSC
{
public String MscID;
public String MscName;
public String MscCompany;
public float MscLongitude;
public float MscLatitude;
public float MscAltitude;
public MSC(String MscID, String MscName, String MscCompany,
float MscLongitude, float MscLatitude,float MscAltitude){
this.MscID = MscID;
this.MscName = MscName;
this.MscCompany = MscCompany;
this.MscLongitude =MscLongitude;
this.MscLatitude = MscLatitude;
this.MscAltitude = MscAltitude;
}
}
public class sqlserverjdbc {
public Connection getConnection(){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加載JDBC驅(qū)動(dòng)
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=gsm"; //連接服務(wù)器和數(shù)據(jù)庫(kù)sample
String userName = "sa"; //默認(rèn)用戶(hù)名
String userPwd = "123"; //密碼
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn =DriverManager.getConnection(dbURL, userName, userPwd);
} catch (Exception e) {
e.printStackTrace();
}
return dbConn;
}
public void printUserInfo(){
Connection con = getConnection();
Statement sta = null;
ResultSet rs = null;
System.out.println("打印表格MSC信息");
try {
sta = con.createStatement();
rs = sta.executeQuery("select * from MSC信息");
System.out.println("MscID\tMscName\tMscCompany\tMscLongitude\tMscLatitude\tMscAltitude");
while(rs.next()){
System.out.println(rs.getString("MscID")+"\t"+
rs.getString("MscName")+"\t"+
rs.getString("MscCompany")+"\t"+
rs.getFloat("MscLongitude")+"\t"+
rs.getFloat("MscLatitude")+"\t"+
rs.getFloat("MscAltitude"));
}
con.close();
sta.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("打印完成\n");
}
public void delete(String MscID){
Connection con = getConnection();
String sql = "delete from MSC信息 where MscID = " + MscID;
PreparedStatement pst;
System.out.println("刪除表格MSC信息中 ID = "+MscID+"的記錄");
try {
pst = con.prepareStatement(sql);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("記錄刪除失?。。。?);
}
System.out.println("記錄刪除成功?。?!\n");
}
public void insert(MSC msc){
Connection con = getConnection();
String sql = "insert into MSC信息 values(?,?,?,?,?,?)";
PreparedStatement pst;
System.out.println("插入一條記錄");
try {
pst = con.prepareStatement(sql);
pst.setString(1, msc.MscID);
pst.setString(2, msc.MscName);
pst.setString(3, msc.MscCompany);
pst.setFloat(4, msc.MscLongitude);
pst.setFloat(5, msc.MscLatitude);
pst.setFloat(6, msc.MscAltitude);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("插入失?。。?!");
}
System.out.println("插入成功?。?!\n");
}
//更新MscID的MscName
public void updateMscName(String MscID, String MscName){
Connection con = getConnection();
String sql = "update MSC信息 set MscName = ? where MscID = ?";
PreparedStatement pst;
System.out.println("修改一條記錄");
try {
pst = con.prepareStatement(sql);
pst.setString(1, MscName);
pst.setString(2, MscID);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改失?。。?!");
}
System.out.println("修改成功?。?!\n");
}
public static void main(String args[]){
sqlserverjdbc sql = new sqlserverjdbc();
sql.printUserInfo();
sql.delete("1111");
sql.printUserInfo();
sql.updateMscName("5215", "聯(lián)想");
sql.printUserInfo();
sql.insert(new MSC("1111", "中興" ," 中興", (float)12.2, (float)3.4,(float)45.5));
sql.printUserInfo();
}
}
1)每個(gè)員工的信息包括:編號(hào)、姓名、性別、出生年月、學(xué)歷、職務(wù)、電話(huà)、住址等。
(2)系統(tǒng)的功能包括:
(a)查詢(xún):按特定條件查找員工。
(b)修改:按編號(hào)對(duì)某個(gè)員工的某項(xiàng)信息進(jìn)行修改。
(c)插入:加入新員工的信息。
(d)刪除:按編號(hào)刪除已離職員工的信息。
(e)排序:按特定條件對(duì)所有員工的信息進(jìn)行排序。
作為一個(gè)學(xué)生,需要增強(qiáng)自己的動(dòng)手能力哦,不然這樣的設(shè)計(jì)會(huì)變得毫無(wú)用處。下面這個(gè)可以學(xué)習(xí)下
網(wǎng)頁(yè)鏈接