eclipse中连接数据库statement和preparestatement方法

1、创建一个java project,再创建一个类,后面使用。

2、源代码statement方法

package jdbc001;  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;  
public class JDBC {
    final String URL="jdbc:sqlserver://localhost:1433;DatabaseName=student";
    final String USERNAME="sa";
    String PWD = "909911" ; 
    public void update(){ 
        Connection connection=null; 
        Statement stmt=null;
        //重点:注意导入的是import java.sql.Statement包
        try {
            //加载驱动类
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
            //建立连接
            connection = DriverManager.getConnection(URL,USERNAME,PWD);
            //sql语句
            String sql="insert into student values(1710252276,'海棠',18,'河南')"; 
            //增删改 
            stmt =connection.createStatement();
            //执行sql语句
            int count = stmt.executeUpdate(sql);
            //判断是否成功
            if(count>0){
                System.out.println("增加成功"); 
            } 
            //查询操作 //模糊查询
            String sqla="select * from student where sname like '%'+'海'+'%'";
            ResultSet rs=null;
            rs= stmt.executeQuery(sqla);
            //处理结果
            while(rs.next()) {
            	int sno=rs.getInt("sno");
            	String sname = rs.getString("sname");
            	int int1 = rs.getInt(3);
            	System.out.println(sno+sname+int1);
            }
            
            
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace(); 
        }  
        catch ( Exception e) {
            e.printStackTrace();
        }
        finally { 
        	try {  
				if(stmt!=null)stmt.close();
				if(stmt!=null)connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
        }
         
    }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JDBC jdbc = new JDBC();
		jdbc.update();
	}

}

3、preparestatement方法

package jdbc001;  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;  
public class JDBC2 {
    final String URL="jdbc:sqlserver://localhost:1433;DatabaseName=student";
    final String USERNAME="sa";
    String PWD = "909911" ; 
    public void update(){ 
        Connection connection=null; 
        Statement stmt=null;
        //重点:注意导入的是import java.sql.Statement包
        try {
            //加载驱动类
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
            //建立连接
            connection = DriverManager.getConnection(URL,USERNAME,PWD);
            //sql语句
            String sql="insert into student values(?,?,?,?)"; 
            //增删改 
          //  stmt =connection.createStatement();
            PreparedStatement pstmt = connection.prepareStatement(sql);
            //执行sql语句
            pstmt.setInt(1, 17102);
            pstmt.setString(2, "海棠");
            pstmt.setInt(3, 18);
            pstmt.setString(4, "河南1");
             
            int count = pstmt.executeUpdate();
             
            //判断是否成功
            if(count>0){
                System.out.println("增加成功001"); 
            }  
            
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace(); 
        }  
        catch ( Exception e) {
            e.printStackTrace();
        }
        finally { 
        	try {  
				if(stmt!=null)stmt.close();
				if(stmt!=null)connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
        }
         
    }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JDBC2 jdbc = new JDBC2();
		jdbc.update();
	}

}

4、流程图
eclipse中连接数据库statement和preparestatement方法_第1张图片

完美。

你可能感兴趣的:(java)