JDBC例子

第一个:

 

Java代码
  1. public static void add() throws Exception   
  2.     {   
  3.         final String url = "jdbc:mysql://127.0.0.1:3306/test";   
  4.         final String user = "root";   
  5.         final String pwd = "1234";   
  6.         Connection cn = null;   
  7.         Statement stm = null;   
  8.         try  
  9.         {   
  10.             Class.forName("org.gjt.mm.mysql.Driver");   
  11.             cn = DriverManager.getConnection(url, user, pwd);   
  12.             stm = cn.createStatement();   
  13.             final String sql = " insert into user values(9999999,'123','2008-01-01')";   
  14.             stm.execute(sql);   
  15.         }   
  16.         finally  
  17.         {   
  18.             stm.close();   
  19.             cn.close();   
  20.         }   
  21.     }  
public static void add() throws Exception
	{
		final String url = "jdbc:mysql://127.0.0.1:3306/test";
		final String user = "root";
		final String pwd = "1234";
		Connection cn = null;
		Statement stm = null;
		try
		{
			Class.forName("org.gjt.mm.mysql.Driver");
			cn = DriverManager.getConnection(url, user, pwd);
			stm = cn.createStatement();
			final String sql = " insert into user values(9999999,'123','2008-01-01')";
			stm.execute(sql);
		}
		finally
		{
			stm.close();
			cn.close();
		}
	}

 

 

第二个:

Java代码
  1. package dao;   
  2.   
  3. import java.sql.Connection;   
  4. import java.sql.DriverManager;   
  5. import java.sql.PreparedStatement;   
  6. import java.sql.ResultSet;   
  7. import java.sql.SQLException;   
  8. import java.sql.Statement;   
  9.   
  10. /**  
  11.  * @author zl JDBC连数据库的例子  
  12.  *   
  13.  */  
  14. public class JDBC   
  15. {   
  16.     public static Connection getConnection()   
  17.     {   
  18.         final String url = "jdbc:mysql://127.0.0.1:3306/test";   
  19.         final String sUsr = "root";   
  20.         final String sPwd = "1234";   
  21.         try  
  22.         {   
  23.             Class.forName("org.gjt.mm.mysql.Driver");   
  24.             return DriverManager.getConnection(url, sUsr, sPwd);   
  25.         }   
  26.         catch (final ClassNotFoundException e)   
  27.         {   
  28.             //TODO 找不到驱动   
  29.         }   
  30.         catch (final SQLException e)   
  31.         {   
  32.             //TODO 创建连接异常   
  33.         }   
  34.         return null;   
  35.     }   
  36.   
  37.        
  38.   
  39.     public static void get() throws SQLException   
  40.     {   
  41.         final String sql = "SELECT id,name FROM user where id=?";   
  42.         PreparedStatement pstmt = null;   
  43.         final Connection cn = getConnection();   
  44.         try  
  45.         {   
  46.             pstmt = cn.prepareStatement(sql);   
  47.             pstmt.setInt(1468000);   
  48.             final ResultSet rs = pstmt.executeQuery();   
  49.             while (rs.next())   
  50.             {   
  51.                 System.out.print(rs.getString("id"));   
  52.                 System.out.print(":");   
  53.                 System.out.print(rs.getString("name") + "\r\n");   
  54.             }   
  55.             rs.close();   
  56.             pstmt.close();   
  57.         }   
  58.   
  59.         finally  
  60.         {   
  61.             try  
  62.             {   
  63.                 cn.close();   
  64.             }   
  65.             catch (final SQLException e)   
  66.             {   
  67.                 e.printStackTrace();   
  68.             }   
  69.         }   
  70.     }   
  71.   
  72.     public static void main(final String[] args) throws Exception   
  73.     {   
  74.         get();   
  75.         //add();   
  76.     }   
  77. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值