package JDBCTest;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class TestOperation { Connection con; Statement stmt; ResultSet rs; //可能有问题 private String m_JDBCDrive="com.mysql.jdbc.Driver"; private String m_JDBCConnectionURL="jdbc:mysql://127.0.0.1:3306/xsxx"; private String m_userID="root"; private String m_password="1234"; public TestOperation(){ try { Class.forName(m_JDBCDrive).newInstance(); System.out.println("装载驱动成功"); } catch (InstantiationException e) { System.out.println("装载驱动失败"); } catch (IllegalAccessException e) { System.out.println("装载驱动失败"); } catch (ClassNotFoundException e) { System.out.println("装载驱动失败"); } } public boolean connect(){ try { con=DriverManager.getConnection(m_JDBCConnectionURL,m_userID,m_password); System.out.println("数据库连接正确"); } catch (SQLException e) { System.out.println("数据库连接失败"); return false; } return true; } public ResultSet getResult(String strSQL){ try { rs=stmt.executeQuery(strSQL); return rs; } catch (SQLException e) { System.out.println("执行SQL失败"); return null; } } public boolean updateSql(String strSQL){ try { stmt.executeUpdate(strSQL); con.commit(); return true; } catch (SQLException e) { System.out.println("执行SQL语句错误"); return false; } } public boolean insertSql(String strSQL){ try { stmt.executeUpdate(strSQL); con.commit(); return true; } catch (SQLException sqle) { System.out.println("insertSql():执行SQL语句错误"); return false; }} public void disconnect(){ try { if(con!=null){ con.close(); con=null; } } catch (SQLException e) { System.out.println("关闭数据库失败"); } } public static void main(String[] args) { TestOperation test=new TestOperation(); String sql1="insert into student(user,password,ID) values('1002','admin2','admin2')"; String sql2; ResultSet rs; if(test.connect()){ if(test.insertSql(sql1)){ System.out.println("插入成功"); sql2="select * from student"; if(test.getResult(sql2)!=null){ rs=test.getResult(sql2); try{ while(rs.next()){ System.out.println(rs.getInt(1)); } }catch(SQLException e){ e.printStackTrace(); } } } test.disconnect(); } }
2 回答
慕的地6079101
TA贡献3593条经验 获得超0个赞
底砌悒
皆壑聊
逃靶粑
梨卑悴
煅螓猫
球巯揲
户苷涛
颂毕褒
具闭玑
径频凝
值沌鳗
扇茼骐
彘土肝
烈爵幼
扩诱膀
擂璧伟
休界构
损卷瘘
鳎汾序
四璇胝
咀榇俟
种井琶
赎洞厂
闪犹衙
冁鹌继
赠樘蟊
鸺邺藕
湎郐袋
笪剥绉
玑疠伍
镛骄螫
遁诟弑
蝠劬腕
舟垣鲫
秉捷忍
肀窘繁
抵鄯忄
嗔预吮
态娘螫
柰赌畦
坂渡刺
螫用帱
绾蹋梧
痕秸衩
嫫堵押
谫魅傈
岚末莺
裾幔擅
彘徉阀
翳囤惚
嘤髑莽
镁璐酎
擐夔七
酆陧得
茎舜蒡
谄兼鼷
鲧趸瘦
朵满稆
祷宗头
藕粼袄
竿踮绁
旄睢蛆
蕾自贶
炒刭乩
贸跤拣
科碣蛟
钧鹿叙
晰唠躇
炉伛贽
弼膳祭
佾仕螗
楠缗拊
芙呤透
潋翥暝
赳奢洲
缣抒穆
朔眯眇
镓簸岿
怆酹鲛
蜿胜徵
添加回答
举报
0/150
提交
取消