日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

myeclipse链接数据库,SQL语句实现增删改查

發布時間:2023/12/10 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 myeclipse链接数据库,SQL语句实现增删改查 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、創建properties文件

?

#oracle
#driver=oracle.jdbc.OracleDriver
#url=jdbc\:oracle\:thin\:@localhost\:1521\:xe
#username=system
#password=manager

#mysql
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://192.168.0.121\:3306/test?user\=root&password\=root&characterEncoding\=utf8
username=root
password=root
?

2、創建鏈接數據庫類

public class JdbcConnect {
? ? ? ? private static Properties prop = new Properties();
?? ? ? ?static{
?? ? ? ??? ? ?
?? ? ? ??? ? ?try {
?? ? ? ??? ? ? ?//Properties prop = new Properties();
?? ??? ??? ??? ?prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties"));
?? ??? ??? ? ? ?Class.forName(prop.getProperty("driver"));
?? ??? ??? ??? ?//Class.forName(prop.getProperty("mysqlDriver"));
?? ? ? ??? ? ?} catch (Exception e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ? ? ??? ??? ?System.out.println("Error");
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ? ? ??? ??
?? ? ? ?}
?? ? ? ?
?? ? ? ?
?? ? ? ?public static Connection getConn(){
?? ? ? ? ? ?Connection conn =null;
?? ??? ? ? ?try {
?? ??? ? ? ??? ?//Oracle
?? ??? ? ? ? ??? ?conn = DriverManager.getConnection(prop.getProperty("url"),
?? ??? ??? ? ? ?prop.getProperty("username"),prop.getProperty("password"));
?? ??? ? ? ??? ?//Mysql
?? ??? ? ? ??? ?//conn = DriverManager.getConnection(prop.getProperty("mysqlUrl"));
?? ??? ? ? ??? ?//System.out.println(conn);
?? ??? ? ? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}finally{
?? ??? ??? ?
?? ??? ?}
?? ? ? ? return conn; ??
?? ??? ? ??
?? ? ? ??? ?
?? ? ? ?}
?? ? ? ?
?? ? ? ?
?? ? ? ?public static void ?close(Connection conn,PreparedStatement pstmt,ResultSet rs){
?? ? ? ??? ? try {
?? ??? ??? ??? ? if(pstmt != null)
?? ??? ??? ??? ??? ? pstmt.close();
?? ??? ??? ??? ? if(rs != null)
?? ??? ??? ??? ??? ? rs.close();
?? ??? ??? ??? ? if(conn != null)
?? ??? ??? ??? ??? ? conn.close();
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?// TODO: handle exception
?? ??? ??? ?}
?? ? ? ??? ??
?? ? ? ?}
//?? ? ? ?public static void main(String[] args) {
//?? ??? ??? ? new JdbcConnect().getConn();
//?? ??? ?}
}
?

3、測試訪問數據庫

初始化

? ? private ?Connection conn = null;
? ? private ?PreparedStatement pstmt = null;
? ? private ?ResultSet ?rs = null;

訪問

?? ?//查詢總記錄
?? ?public int queryCount() {
?? ??? ?int count =0;
?? ??? ?try {
?? ??? ??? ?conn = MyJdbcConnect.getConn();
//?? ??? ??? ?pstmt = conn.prepareStatement("select count(*) from student");
?? ??? ??? ?pstmt = conn.prepareStatement("select max(age) as avgvalue from userinfodz");
?? ??? ??? ?rs = pstmt.executeQuery();
?? ??? ??? ?if(rs.next()){
?? ??? ??? ??? ? count = rs.getInt(1);
?? ??? ??? ?}
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO: handle exception
?? ??? ??? ?e.printStackTrace();
?? ??? ?}finally{
?? ??? ??? ?MyJdbcConnect.close(conn, pstmt, rs);
?? ??? ?}
?? ??? ?return count;
?? ?}

?

附:

public interface SQLSentence {
? ? ? ? ? ?String oinsert="insert into mytable values(mytab_seq.nextval,?,?,?,?,?)";
? ? ? ? ? ?String minsert="insert into mytable values(null,?,?,?,?,?)";
? ? ? ? ? ?String update="update mytable ?t set t.myname=? ,t.myage=?,t.myhobby =?,t.myinfo=? where t.myid=?";
? ? ? ? ? ?String delete="delete from mytable where myid=?";
? ? ? ? ? ?String qcount="select count(*) from mytable";
? ? ? ? ? ?String Pagingfororacle="select * from(select T.*,ROWNUM RN from MyTable T where ROWNUM<=? order by T.myid) where RN>=?";
? ? ? ? ? ?String Pagingformysql=" select * from mytable limit ? , ?";
? ? ? ? ? ?String PagingforOracle2 = "select * from(select T.*,ROWNUM RN from MyTable T order by T.myid) where RN between ? and ?";
? ? ? ? ? ?String GetMyTableById = "select * from mytable where myid = ?";?
? ? ? ? ? ?
}
?

?

總結

以上是生活随笔為你收集整理的myeclipse链接数据库,SQL语句实现增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。