JDBC 의 몇 가지 간단 한 예

4786 단어 Java
1. Statement 사용  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class TestJDBCStatementDelete {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan=new Scanner(System.in);
		System.out.println("       id:");
		int id=scan.nextInt();
		Connection con=null;
		Statement stmt=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","root");
			stmt=con.createStatement();
			String sql="delete from emp where id="+id;
			int res=stmt.executeUpdate(sql);
			if(res!=0) 
				System.out.println("    ");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(con!=null)
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(stmt!=null)
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
}

2. Prepared Statement 사용
package com.oracle.test1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJDBCPreparedStatementInsert {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan=new Scanner(System.in);
		System.out.println("         、       :");
		String name=scan.next();
		int sal=scan.nextInt();
		String hiredate=scan.next();
		Connection con=null;
		PreparedStatement stmt=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","root");
			String sql="insert into emp(name,sal,hiredate) "+"values (?,?,?)";
			stmt=con.prepareStatement(sql);
			stmt.setString(1,name);
			stmt.setInt(2,sal);
			stmt.setString(3,hiredate);
			int res=stmt.executeUpdate();
			if(res!=0)
				System.out.println("    ");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(con!=null)
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(stmt!=null)
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
}

3. ResultSetMetaData 와 DatabaseMetaData 의 사용
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;

public class Test {
	public static void main(String[] args) {
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hr", "root", "root");
			String sql = "select * from emp";
			PreparedStatement stmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
			ResultSet rs = stmt.executeQuery();
			//rs.next();
			//rs.deleteRow();
			/*rs.last();
			System.out.println(rs.getRow());
			rs.beforeFirst();
			while(rs.next()){
				System.out.println(rs.getString("name"));
			}*/
			ResultSetMetaData rsmd = rs.getMetaData();
			System.out.println("  :"+rsmd.getColumnCount());
			System.out.println("  :"+rsmd.getColumnName(5));
			System.out.println("     :"+rsmd.getTableName(4));
			
			DatabaseMetaData dbmt=conn.getMetaData();
			System.out.println("     :"+dbmt.getDatabaseProductName());
			System.out.println("     :"+dbmt.getDatabaseProductVersion());
			System.out.println("      :"+dbmt.getDatabaseMajorVersion());
			System.out.println("      :"+dbmt.getDatabaseMinorVersion());
			int type=rsmd.getColumnType(2);
			if(type==Types.INTEGER){
				System.out.println("   :int");
			}else if(type==Types.VARCHAR){
				System.out.println("   :varchar");
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

좋은 웹페이지 즐겨찾기