zoukankan      html  css  js  c++  java
  • jdbc连接mysql

    要导入相应的包]

     1 public class ConnDB {
     2     private Connection ct = null;
     3    //驱动程序名
     4     String driver = "com.mysql.jdbc.Driver";
     5     // URL指向要访问的数据库名scutcs
     6     String url = "jdbc:mysql://127.0.0.1:3306/develop";
     7     // MySQL配置时的用户名
     8     String user = "root";
     9     // Java连接MySQL配置时的密码

    10 String password = "wzy123";
    11 
    12     public Connection getConn(){
    13         try{
    14             Class.forName(driver);    
    15             ct = DriverManager.getConnection(url, user, password);
    16         }
    17         catch(Exception e){
    18             e.printStackTrace();
    19         }
    20         return ct;
    21     }
    22 }
    public int add(String day,String time,
    				String volname,String volclass,String voltel,String volemail){
    			Connection con = DbConnect.this.getConn();
    			int num = -1;
    			
    			try {
    				java.sql.Statement statement = con.createStatement();
    				 num = statement.executeUpdate( "insert into tb_volinfo(day,time,volname,volclass,voltel,volemail) "
    						+ "values('"+day+"','"+time+"','"+volname+"','"+volclass+"','"+voltel+"','"+volemail+"')");
    			
    				if(statement!=null){
    					statement.close();
    				}
    				if(con!=null){
    					con.close();//关闭数据库连接
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    
    			return num;
    		}
    		public boolean SelectVal(String volname,String voltel){
    			Connection con1 = DbConnect.this.getConn();
    			ResultSet result =null;
    			
    			try {
    				java.sql.Statement statement = con1.createStatement();
    			    result = statement.executeQuery("select * from tb_volinfo where volname='"+volname+"' and voltel='"+voltel+"';");
    				while(result.next()){
    					return true;
    				}
    			    if(statement!=null){
    					statement.close();
    				}
    				if(con1!=null){
    					con1.close();//关闭数据库连接
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    			return false;
    		}
    

      

  • 相关阅读:
    真机分享文件到虚拟机的centos 7 分享文件位置记录
    Linux系统列出某个用户组里的所有用户命令
    笔记之Linux命令vi
    笔记之Linux系统文件管理命令
    我的C#
    消息框
    ddt数据驱动在ui自动化中的应用二【多测师】
    ddt数据驱动在ui自动化中的应用一【多测师】
    基于ddt+unittest+Excel做接口测试自动化测试【多测师】
    Python操作MD5加密【多测师】
  • 原文地址:https://www.cnblogs.com/wwzyy/p/4952768.html
Copyright © 2011-2022 走看看