JDBC学习笔记(1)-----温故jdbc

一、使用MySQL建表:





二、编写java程序:

1、添加MySQL的jdbc类库



2、加载MySQL的jdbc驱动

public static void main(String[] args)  
	   {  
	       try  
	       {  
	    	   Class.forName("com.mysql.jdbc.Driver");//加载mysql数据库驱动程序  
	    	   System.out.println("Success loading Mysql Drives");  
	       }  
	       catch (Exception e)  
	    	{  
	        	System.out.println("Errot loading Mysql Driver!");  
	        	e.printStackTrace();  
	    	}  
	   }

3、建立连接:

DriverManager.getConnection(url, user, password);




Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","7424994");

4、创建sql对象:

Statement statement = ct.createStatement();
执行插入sql语句:
statement.executeUpdate("insert into student values" +"('100003','zzx','249974',21,'810620174@qq.com')");

完整代码:

package com.zzx.jdbcTest;

import java.sql.*;

public class TestJdbc 
{
	public static void main(String[] args)  
	   {  
		   Connection ct = null;
		   Statement statement = null;
	       try  
	       {  
	    	   Class.forName("com.mysql.jdbc.Driver");//加载mysql数据库驱动程序  
	    	   System.out.println("Success loading Mysql Drives");
	    	   ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","7424994");
	           statement = ct.createStatement();
	           statement.executeUpdate("insert into student values" +
	           		"('100003','zzx','249974',21,'810620174@qq.com')");
	       }  
	       catch (Exception e)  
	    	{  
	        	System.out.println("Errot loading Mysql Driver!");  
	        	e.printStackTrace();  
	    	}  
	       finally
	       {
	    	   if(statement !=null)
	    	   {
	    		   try {
					       statement.close();
	    		   		} 
	    		   catch (Exception e2) 
	    		   		{
					       e2.printStackTrace();
	    		   		}
	    		   statement = null;
	    	   }
	    	   if(ct !=null)
	    	   {
	    		   try {
					       ct.close();
	    		   		} 
	    		   catch (Exception e2) 
	    		   		{
					       e2.printStackTrace();
	    		   		}
	    		   ct = null;
	    	   }
	       }
	   }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值