新聞中心
java如何實(shí)現(xiàn)sql連接和查詢(xún)的代碼?
import java.sql.Connection。
成都創(chuàng)新互聯(lián)公司-成都網(wǎng)站建設(shè)公司,專(zhuān)注成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷(xiāo)推廣,主機(jī)域名,網(wǎng)頁(yè)空間,網(wǎng)站改版維護(hù)有關(guān)企業(yè)網(wǎng)站制作方案、改版、費(fèi)用等問(wèn)題,請(qǐng)聯(lián)系成都創(chuàng)新互聯(lián)公司。
import java.sql.DriverManager; ?
import java.sql.PreparedStatement; ?
import java.sql.ResultSet; ?
import java.sql.SQLException;
import javax.naming.Context; ?
import javax.naming.InitialContext; ?
import javax.naming.NamingException; ?
import javax.sql.DataSource;
public class DBCon {
//數(shù)據(jù)庫(kù)驅(qū)動(dòng)對(duì)象
public static final String DRIVER="oracle.jdbc.driver.OracleDriver";
//數(shù)據(jù)庫(kù)連接地址(數(shù)據(jù)庫(kù)名)
public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
//登陸名
public static final String USER="FM";
//登陸密碼
public static final String PWD="FM";
//創(chuàng)建數(shù)據(jù)庫(kù)連接對(duì)象
private Connection con=null;
//創(chuàng)建數(shù)據(jù)庫(kù)預(yù)編譯對(duì)象
private PreparedStatement ps=null;
//創(chuàng)建結(jié)果集
private ResultSet rs=null;
//創(chuàng)建數(shù)據(jù)源對(duì)象
public static DataSource source=null;
// ?//靜態(tài)代碼塊 ?
// ?static{ ?
// ?
// ? ? ?//初始化配置文件context ?
// ? ? ?try { ?
// ? ? ? ? ?Context context=new InitialContext(); ?
// ? ? ? ? ?source=(DataSource)context.lookup("java:comp/env/jdbc/webmessage"); ?
// ? ? ?} catch (Exception e) { ?
// ? ? ? ? ?// TODO Auto-generated catch block ?
// ? ? ? ? ?e.printStackTrace(); ?
// ? ? ?} ?
// ?
// ?
// ?}
/**
* 獲取數(shù)據(jù)庫(kù)連接
*/
public Connection getCon(){
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=DriverManager.getConnection(URL,USER,PWD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
} ?
// ?/** ?
// ? * 獲取數(shù)據(jù)庫(kù)連接 ?
// ? */ ?
// ?public Connection getCon(){ ?
// ?
// ? ? ?try { ?
// ? ? ? ? ?con=source.getConnection(); ?
// ? ? ?} catch (SQLException e) { ?
// ? ? ? ? ?// TODO Auto-generated catch block ?
// ? ? ? ? ?e.printStackTrace(); ?
// ? ? ?} ?
// ?
// ? ? ?return con; ?
// ?} ?
/**
* 關(guān)閉所有資源
*/
public void closeAll(){
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ps!=null)
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} ?
}
/**
* @param sql數(shù)據(jù)庫(kù)更新(增、刪、改) 語(yǔ)句
* @param pras參數(shù)列表(可傳,可不傳,不傳為NULL,以數(shù)組形式存在)
* @return 返回受影響都行數(shù)
*/
public int update(String sql,String... pras){
int resu=0;
con=getCon();
try {
ps=con.prepareStatement(sql);
for(int i=0;ipras.length;i++){
ps.setString(i+1,pras[i]);
}
resu=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
closeAll();
}
return resu;
}
/**
* @param sql數(shù)據(jù)庫(kù)查詢(xún)語(yǔ)句
* @param pras參數(shù)列表(可傳,可不傳,不傳為NULL,以數(shù)組形式存在)
* @return 返回結(jié)果集
*/
public ResultSet query(String sql,String... pras){
con=getCon();
try {
ps=con.prepareStatement(sql);
if(pras!=null)
for(int i=0;ipras.length;i++){
ps.setString(i+1, pras[i]);
}
rs=ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
} ?
}
JAVA中怎么查詢(xún)代碼?
try{Connection con;\x0d\x0a Statement stmt;\x0d\x0a ResultSet rs;\x0d\x0a int temp;\x0d\x0a Class.forName("com.mysql.jdbc.Driver");\x0d\x0a con=DriverManager.getConnection("jdbc:mysql://localhost:3306/java","root","");//以上是數(shù)據(jù)庫(kù)連接,不同的數(shù)據(jù)管理器有 //不同的驅(qū)動(dòng)和鏈接方式,以上是mysql的連接\x0d\x0astmt=con.createStatement();\x0d\x0a rs=stmt.executeQuery("select * from student");//執(zhí)行查詢(xún)語(yǔ)句,結(jié)果賦值給結(jié)果集rs\x0d\x0a //結(jié)果集是結(jié)果于字段編號(hào)的映射,每一個(gè)字\x0d\x0a //段都有一個(gè)編號(hào),最小為1,也就是第一個(gè)字段 \x0d\x0a while(rs.next()){\x0d\x0a String names=rs.getString("name");//查詢(xún)結(jié)果轉(zhuǎn)換成字符串。\x0d\x0a \x0d\x0a System.out.println(names);\x0d\x0a\x0d\x0a}rs.close();\x0d\x0a }catch(Exception e){\x0d\x0a e.printStackTrace();\x0d\x0a }
SSM框架,根據(jù)id查詢(xún)一條數(shù)據(jù)的java代碼怎么寫(xiě)
查詢(xún)語(yǔ)句是要寫(xiě)在xml文件中的,如select * from table_name where id = #{id},#{id}表示取值。比如你在前臺(tái)傳來(lái)一個(gè)id,在后臺(tái)接收到這個(gè)id,然后通過(guò)方法findById(String id)去查,此時(shí)#{id},取的就是這個(gè)id值
網(wǎng)站標(biāo)題:查詢(xún)數(shù)據(jù)的java代碼 查詢(xún)數(shù)據(jù)的java代碼是多少
網(wǎng)站路徑:http://fisionsoft.com.cn/article/doidoio.html