原生jdbc连接SQL Server数据库

it2022-05-05  156

接下来看代码块: 用的是Java.sql的包,不要用错包了 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class SQL_ServerJdbc { public static void main(String[] args) { //声明变量 // Statemert不支持SQL语句带参数 Statement sta=null; Connection conn=null; ResultSet re =null; // 1、加载架包 try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 2、通过DriverManager获取数据库的链接 String url ="jdbc:sqlserver://localhost:1433;databaseName=WisdomCommerce";//连接地址 String user ="sa";//用户 String password ="sql123";//密码 conn= DriverManager.getConnection(url, user, password); System.out.println(conn); conn.setAutoCommit(false);//设置为手动提交 sta =conn.createStatement(); sta.addBatch("DELETE FROM Ba_Property3 WHERE Property3ID>6;"); sta.executeBatch(); conn.commit(); System.out.println(sta); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(sta!=null) { sta.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(sta!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

 


最新回复(0)