java訪問mysql資料庫的方法,javamysql資料庫

來源:互聯網
上載者:User

java訪問mysql資料庫的方法,javamysql資料庫

1、下載介面程式包mysql-connector-java-5.0.8-bin.jar 

2、編程

(1)載入驅動

(2)編程串連操作

(3)返回結果處理


編程樣本

import java.sql.*;public class Access2Database{public Connection getConn(){Connection conn=null;try{Class.forName("com.mysql.jdbc.Driver");String url="jdbc:mysql://localhost:3306/mytest";String user="root";String password="111";conn=DriverManager.getConnection(url, user, password);if(conn!=null){System.out.println("The connection to database is successful!");}}catch(Exception e){e.printStackTrace();}return conn;}public ResultSet getResultSet(Statement stam,String sql){ResultSet res=null;try {res=stam.executeQuery(sql);} catch (SQLException e){e.printStackTrace();}return res;}void showResultSet(ResultSet res){}}
import java.sql.*;public class GetConnection{public static void main(String[] args){Access2Database adb=new Access2Database();Connection conn=adb.getConn();Statement stam=null;try {stam = conn.createStatement();} catch (SQLException e1) {e1.printStackTrace();}//show resultsetString sql="select * from student;";ResultSet res=adb.getResultSet(stam, sql);try {System.out.println("name\tmajor\tscore");while(res.next()){String name,major;int score;name=res.getString(1);major=res.getString(2);score=res.getInt(3);System.out.println(name+"\t"+major+"\t"+score);}} catch (SQLException e) {e.printStackTrace();}try{res.close();}catch(SQLException e){e.printStackTrace();}//insert something into tablesql="insert into student(name,major,score) values('f','Chinese','70');";try {stam.execute(sql);} catch (SQLException e) {e.printStackTrace();}//delete something from the tablesql="delete from student where name='f';";try{stam.executeUpdate(sql);}catch(SQLException e){e.printStackTrace();}//change the data int the tablesql="update student set score=100 where name='a' and major='Chinese'";try{stam.executeUpdate(sql);}catch(SQLException e){e.printStackTrace();}//prepared statementsql="select * from student where name=?";PreparedStatement pstam=null;try {pstam=conn.prepareStatement(sql);pstam.setString(1, "a");res=pstam.executeQuery();System.out.println("**********************");while(res.next()){String name,major;int score;name=res.getString(1);major=res.getString(2);score=res.getInt(3);System.out.println(name+"\t"+major+"\t"+score);}} catch (SQLException e) {e.printStackTrace();}//release the resource of the programtry{res.close();pstam.close();stam.close();conn.close();}catch(SQLException e){e.printStackTrace();}}}
按需調整代碼即可

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.