DBMNG数据库管理与应用

所有存在都是独创。
当前位置:首页 > MySQL > 驱动连接

java访问mysql数据库的方法

1、下载接口程序包mysql-connector-java-5.0.8-bin.jar 下载地址

2、编程

(1)加载驱动

(2)编程连接操作

(3)返回结果处理


编程示例


[java] view plaincopy
  1. import java.sql.*;  
  2.   
  3. public class Access2Database{  
  4.     public Connection getConn(){  
  5.         Connection conn=null;  
  6.         try{  
  7.             Class.forName("com.mysql.jdbc.Driver");  
  8.             String url="jdbc:mysql://localhost:3306/mytest";  
  9.             String user="root";  
  10.             String password="111";  
  11.             conn=DriverManager.getConnection(url, user, password);  
  12.             if(conn!=null){  
  13.                 System.out.println("The connection to database is successful!");  
  14.             }  
  15.         }catch(Exception e){  
  16.             e.printStackTrace();  
  17.         }  
  18.         return conn;  
  19.     }  
  20.       
  21.     public ResultSet getResultSet(Statement stam,String sql){  
  22.         ResultSet res=null;  
  23.         try {  
  24.             res=stam.executeQuery(sql);  
  25.         } catch (SQLException e){  
  26.             e.printStackTrace();  
  27.         }  
  28.         return res;  
  29.     }  
  30.     void showResultSet(ResultSet res){}  
  31. }  
[java] view plaincopy
  1. import java.sql.*;  
  2.   
  3. public class GetConnection{  
  4.     public static void main(String[] args){  
  5.         Access2Database adb=new Access2Database();  
  6.         Connection conn=adb.getConn();  
  7.         Statement stam=null;  
  8.         try {  
  9.             stam = conn.createStatement();  
  10.         } catch (SQLException e1) {  
  11.             e1.printStackTrace();  
  12.         }  
  13.           
  14.         //show resultset  
  15.         String sql="select * from student;";  
  16.         ResultSet res=adb.getResultSet(stam, sql);  
  17.         try {  
  18.             System.out.println("name\tmajor\tscore");  
  19.             while(res.next()){  
  20.                 String name,major;  
  21.                 int score;  
  22.                 name=res.getString(1);  
  23.                 major=res.getString(2);  
  24.                 score=res.getInt(3);  
  25.                 System.out.println(name+"\t"+major+"\t"+score);  
  26.             }  
  27.         } catch (SQLException e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.         try{  
  31.         res.close();  
  32.         }catch(SQLException e){  
  33.             e.printStackTrace();  
  34.         }  
  35.           
  36.         //insert something into table  
  37.         sql="insert into student(name,major,score) values('f','Chinese','70');";  
  38.         try {  
  39.             stam.execute(sql);  
  40.         } catch (SQLException e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.           
  44.         //delete something from the table  
  45.         sql="delete from student where name='f';";  
  46.         try{  
  47.             stam.executeUpdate(sql);  
  48.         }catch(SQLException e){  
  49.             e.printStackTrace();  
  50.         }  
  51.           
  52.         //change the data int the table  
  53.         sql="update student set score=100 where name='a' and major='Chinese'";  
  54.         try{  
  55.             stam.executeUpdate(sql);  
  56.         }catch(SQLException e){  
  57.             e.printStackTrace();  
  58.         }  
  59.           
  60.         //prepared statement  
  61.         sql="select * from student where name=?";  
  62.         PreparedStatement pstam=null;  
  63.         try {  
  64.             pstam=conn.prepareStatement(sql);  
  65.             pstam.setString(1"a");  
  66.             res=pstam.executeQuery();  
  67.             System.out.println("**********************");  
  68.             while(res.next()){  
  69.                 String name,major;  
  70.                 int score;  
  71.                 name=res.getString(1);  
  72.                 major=res.getString(2);  
  73.                 score=res.getInt(3);  
  74.                 System.out.println(name+"\t"+major+"\t"+score);  
  75.             }  
  76.         } catch (SQLException e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.           
  80.         //release the resource of the program  
  81.         try{  
  82.             res.close();  
  83.             pstam.close();  
  84.             stam.close();  
  85.             conn.close();  
  86.         }catch(SQLException e){  
  87.             e.printStackTrace();  
  88.         }  
  89.     }  
  90. }  
按需调整代码即可
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号