java与MySQL数据库的连接

java与MySQL数据库的连接

1.数据库的安装和建立参见上一篇博客中的第1,2步骤。(http://blog.csdn.net/nuptboyzhb/article/details/8043091)

或使用SQL语句

[sql] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. # ubuntu Linux  
  2. sudo mysql -u root -p  
  3. #Windows 7(mysql.exe)  
  4.   
  5. create database testdb;  
  6. use testdb;  
  7. CREATE TABLE `name_table` (  
  8.   `_id` int(11) NOT NULL,  
  9.   `name` varchar(32) CHARACTER SET utf8,  
  10.   `age` int(11) NOT NULL,  
  11.   `work` varchar(32) CHARACTER SET utf8,  
  12.   `others` varchar(512) CHARACTER SET utf8 DEFAULT NULL,  
  13.   PRIMARY KEY (`_id`)  
  14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  


2.Eclipse的配置。

导入包mysql-connector-java-5.0.5-bin.jar


3.java代码的编写

[java code]

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /* 
  2.  *@author: ZhengHaibo   
  3.  *web:     blog.csdn.net/nuptboyzhb 
  4.  *mail:    zhb931706659@126.com 
  5.  *2012-10-6  Nanjing njupt 
  6.  */  
  7. import java.sql.Connection;  
  8. import java.sql.DriverManager;  
  9. import java.sql.PreparedStatement;  
  10. import java.sql.ResultSet;  
  11. import java.sql.SQLException;  
  12. public class helloworld {  
  13.     private Connection conn = null;  
  14.     PreparedStatement statement = null;  
  15.     // connect to MySQL  
  16.     void connSQL() {  
  17.         String urle = "jdbc:mysql://localhost:3306/testdb";//port:3306 database:testdb  
  18.         String username = "root";//user  
  19.         String password = "931706659";//password  
  20.         try {   
  21.             Class.forName("com.mysql.jdbc.Driver" );//加载驱动,连接数据库  
  22.             conn = DriverManager.getConnection(urle,username, password );   
  23.             }  
  24.         //捕获加载驱动程序异常  
  25.          catch ( ClassNotFoundException cnfex ) {  
  26.              System.err.println(  
  27.              "装载 JDBC/ODBC 驱动程序失败。" );  
  28.              cnfex.printStackTrace();   
  29.          }   
  30.          //捕获连接数据库异常  
  31.          catch ( SQLException sqlex ) {  
  32.              System.err.println( "无法连接数据库" );  
  33.              sqlex.printStackTrace();  
  34.          }  
  35.     }  
  36.   
  37.     // disconnect to MySQL  
  38.     void deconnSQL() {  
  39.         try {  
  40.             if (conn != null)  
  41.                 conn.close();  
  42.         } catch (Exception e) {  
  43.             System.out.println("关闭数据库问题 :");  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47.   
  48.     // execute selection language  
  49.     ResultSet selectSQL(String sql) {  
  50.         ResultSet rs = null;  
  51.         try {  
  52.             statement = conn.prepareStatement(sql);  
  53.             rs = statement.executeQuery(sql);  
  54.         } catch (SQLException e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.         return rs;  
  58.     }  
  59.   
  60.     // execute insertion language  
  61.     boolean insertSQL(String sql) {  
  62.         try {  
  63.             statement = conn.prepareStatement(sql);  
  64.             statement.executeUpdate();  
  65.             return true;  
  66.         } catch (SQLException e) {  
  67.             System.out.println("插入数据库时出错:");  
  68.             e.printStackTrace();  
  69.         } catch (Exception e) {  
  70.             System.out.println("插入时出错:");  
  71.             e.printStackTrace();  
  72.         }  
  73.         return false;  
  74.     }  
  75.     //execute delete language  
  76.     boolean deleteSQL(String sql) {  
  77.         try {  
  78.             statement = conn.prepareStatement(sql);  
  79.             statement.executeUpdate();  
  80.             return true;  
  81.         } catch (SQLException e) {  
  82.             System.out.println("插入数据库时出错:");  
  83.             e.printStackTrace();  
  84.         } catch (Exception e) {  
  85.             System.out.println("插入时出错:");  
  86.             e.printStackTrace();  
  87.         }  
  88.         return false;  
  89.     }  
  90.     //execute update language  
  91.     boolean updateSQL(String sql) {  
  92.         try {  
  93.             statement = conn.prepareStatement(sql);  
  94.             statement.executeUpdate();  
  95.             return true;  
  96.         } catch (SQLException e) {  
  97.             System.out.println("插入数据库时出错:");  
  98.             e.printStackTrace();  
  99.         } catch (Exception e) {  
  100.             System.out.println("插入时出错:");  
  101.             e.printStackTrace();  
  102.         }  
  103.         return false;  
  104.     }  
  105.     // show data in ju_users  
  106.     void layoutStyle2(ResultSet rs) {  
  107.         System.out.println("-----------------");  
  108.         System.out.println("执行结果如下所示:");  
  109.         System.out.println("-----------------");  
  110.         System.out.println(" id" + " " + "name" +" " + "age" + " " +"work"+ " " + "others");  
  111.         System.out.println("-----------------");  
  112.         try {  
  113.             while (rs.next()) {  
  114.                 System.out.println(rs.getInt("_id") + " "  
  115.                         + rs.getString("name") + " "  
  116.                         +rs.getInt("age") + " "  
  117.                         + rs.getString("work")+ " "  
  118.                         + rs.getString("others"));  
  119.             }  
  120.         } catch (SQLException e) {  
  121.             System.out.println("显示时数据库出错。");  
  122.             e.printStackTrace();  
  123.         } catch (Exception e) {  
  124.             System.out.println("显示出错。");  
  125.             e.printStackTrace();  
  126.         }  
  127.     }  
  128.   
  129.     public static void main(String args[]) {  
  130.   
  131.         helloworld h = new helloworld();  
  132.         h.connSQL();  
  133.         String s = "select * from name_table";  
  134.   
  135.         String insert = "insert into name_table(_id,name,age,work,others) values("+15+",'csdn',"+24+",'M.S.','nupt')";  
  136.         String update = "update name_table set age =19 where name= 'zhb'";  
  137.         String delete = "delete from name_table where name= 'csdn'";  
  138.   
  139.         if (h.insertSQL(insert) == true) {  
  140.             System.out.println("insert successfully");  
  141.             ResultSet resultSet = h.selectSQL(s);  
  142.             h.layoutStyle2(resultSet);  
  143.         }  
  144.         if (h.updateSQL(update) == true) {  
  145.             System.out.println("update successfully");  
  146.             ResultSet resultSet = h.selectSQL(s);     
  147.             h.layoutStyle2(resultSet);  
  148.         }  
  149.         if (h.insertSQL(delete) == true) {  
  150.             System.out.println("delete successfully");  
  151.             ResultSet resultSet = h.selectSQL(s);  
  152.             h.layoutStyle2(resultSet);  
  153.         }  
  154.         h.deconnSQL();  
  155.     }  
  156. }  


整个项目的源代码:http://download.csdn.net/detail/nuptboyzhb/4620059
4.实验结果

[image]


 
 
原文地址:https://www.cnblogs.com/u0mo5/p/4168473.html