MCPcopy Create free account
hub / github.com/Hello-hao/Tbed / RunInsert

Method RunInsert

src/main/java/cn/hellohao/utils/RunSqlScript.java:64–110  ·  view source on GitHub ↗
(String sql)

Source from the content-addressed store, hash-verified

62
63 //insert操作
64 public static Integer RunInsert(String sql) {
65 Integer count = 0;
66 //数据库连接对象
67 Connection conn = null;
68 //数据库操作对象
69 Statement stmt = null;
70 //1、加载驱动程序
71 try {
72 Class.forName(DBDRIVER);
73 } catch (ClassNotFoundException e) {
74 e.printStackTrace();
75 Print.Normal("SqlHelper.java - ClassNotFoundException");
76 }
77 //2、连接数据库
78 //通过连接管理器连接数据库
79 try {
80 //在连接的时候直接输入用户名和密码才可以连接
81 conn = DriverManager.getConnection(DBURL,USERNAME,PASSWORD);
82 } catch (SQLException e) {
83 e.printStackTrace();
84 Print.Normal("SqlHelper.java - SQLException1");
85 }
86 try {
87 stmt = conn.createStatement();
88 } catch (SQLException e) {
89 e.printStackTrace();
90 Print.Normal("SqlHelper.java - SQLException2");
91 }
92 //4、执行语句
93 try {
94 count = stmt.executeUpdate(sql);
95 } catch (SQLException e) {
96 if(e.getMessage().contains("Duplicate key name") && e.getMessage().contains("index_md5key_url")){
97 Print.Normal("Database index already exists, skip this step.");
98 }else{
99 e.printStackTrace();
100 }
101 }
102 //5、关闭操作
103 try {
104 stmt.close();
105 conn.close();
106 } catch (SQLException e) {
107 Print.Normal("SqlHelper.java - SQLException4");
108 }
109 return count;
110 }
111
112
113}

Callers 1

afterPropertiesSetMethod · 0.95

Calls 1

NormalMethod · 0.95

Tested by

no test coverage detected