标签:
/*
在数据库中创建一个Employee的类
create table Employee( id int primary key, name varchar(20), age int);
*/
import java.sql.*; public class JDBCTest { public static void main(String[] args){ String user="user1"; String passwd="pwd1"; String utl="jdbc:mysql://localhost:3306/Test"; String driver="com.mysql.jdbc.Driver"; Connection con=null; Statement stmt=null; ResultSet rs=null; try{ try { Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } con=DriverManager.getConnection(utl,user,passwd); stmt=con.createStatement(); stmt.execute("insert into Employee values(1,‘James1‘,25)"); stmt.execute("insert into Employee values(2,‘James2‘,26)"); rs=stmt.executeQuery("select * from Empolyee");//访问结果集SQL对象 while(rs.next()){ System.out.println(rs.getInt(1)+""+rs.getString(2)+""+rs.getInt(3)); } } catch(SQLException e1){ e1.printStackTrace(); }finally{ try{ if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(con!=null) con.close(); }catch(SQLException e){ System.out.println(e.getMessage()); } } } }
操作步骤
标签:
原文地址:http://www.cnblogs.com/xiaoxiaohui2015/p/5814676.html