首先你得確定你的數(shù)據(jù)庫連接是通過什么形式連接的,hibernate還是原生態(tài)的jdbc 還是spring;
海州網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),海州網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為海州成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的海州做網(wǎng)站的公司定做!
如果是只有hibernate,那么你得通過加載配置文件得到sessionFactory,然后得到session
如果spring,那么同樣也需要注入sessionfactory到你的dao
如果是jdbc方式,那么你就按照原生態(tài)jdbc寫法
總之,在你構(gòu)造DAO時,得有數(shù)據(jù)源。這樣才能操縱你的數(shù)據(jù)庫
如果搞懂了這些問題,那么你的第一個,第三個問題就迎刃而解了。至于第二問題,我沒明白你什么意思!
對數(shù)據(jù)庫進行增刪改查?
以下是 sql server 2013+java.實現(xiàn)的是對MSC對象的增刪改查.
需要下載連接驅(qū)動程序: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ū)動
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=gsm"; //連接服務(wù)器和數(shù)據(jù)庫sample
String userName = "sa"; //默認用戶名
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();
}
}
ResultSet rs=s.executeQuery("select * from teacher"); //查詢語句
s.executeUpdate("update into teacher set ....."); //插入,更新 ,刪除語句
/**
*插入,更新 ,刪除
*主要就是SQL語句不同
*/