.NET 多一点学习,多一点进步
liuming836
全部博文(17)
Linux技术(0)
2011年(1)
2009年(15)
2008年(1)
格伯纳
_dance
wzh5788
rocketlg
GcooQ
comeon09
聖然
shuizhiz
wangbin1
分类: Java
2009-05-03 17:50:17
package com.loginsystem.bean;import java.sql.*;public class ConnectionDataBase{ private static Connection MyConnection; private static Statement MySql; private static ResultSet MyResultSet; //创建数据库的连接. public static void MainConnection() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException e) { System.out.println(e); } try { MyConnection=DriverManager.getConnection("jdbc:odbc:MyDataBase","",""); MySql=MyConnection.createStatement(); } catch(SQLException el) { System.out.println(el); } } //关闭数据库的连接. public static void MyClose() { try { if(MyResultSet!=null) { MyResultSet.close(); } if(MySql!=null) { MySql.close(); } if(MyConnection!=null) { MyConnection.close(); } } catch(SQLException el) { System.out.println(el); } } //检索数据库. public boolean IsExist(String username, String password) { try { String MySqlString="select * from StudentsTable where StuName='"+username+"' and Password='"+password+"'"; MainConnection(); MyResultSet=MySql.executeQuery(MySqlString); if(MyResultSet.next()==false) { MyClose(); return false; } else { MyClose(); return true; } } catch(SQLException el) { //System.out.println(el); return false; } } public void SelectData(String MySqlString) { try { MainConnection(); MyResultSet=MySql.executeQuery(MySqlString); while(MyResultSet.next()) { String StuID=MyResultSet.getString(1); String StuName=MyResultSet.getString(2); String StuSex=MyResultSet.getString(3); System.out.println("----------------------------"); System.out.println("学号:"+StuID); System.out.println("姓名:"+StuName); System.out.println("性别:"+StuSex); } MyClose(); } catch(SQLException el) { System.out.println(el); } } public ResultSet SelectDataSetf(String MySqlString) { try { MainConnection(); MyResultSet=MySql.executeQuery(MySqlString); MyClose(); return MyResultSet; } catch(SQLException el) { System.out.println(el); return null; } } //插入数据. public void InsertData(String MySqlString) { try { MainConnection(); MySql.executeUpdate(MySqlString); System.out.println("A Row Has Been Insertd!"); MyClose(); } catch(SQLException el) { System.out.println(el); } } //更新数据. public static void UpdateData(String MySqlString) { try { MainConnection(); MySql.executeUpdate(MySqlString); System.out.println("A Row Has Been Updated!"); MyClose(); } catch(SQLException el) { System.out.println(el); } } //删除数据库. public void DeleteData(String MySqlString) { try { MainConnection(); MySql.executeUpdate(MySqlString); System.out.println("A Row Has Been Deleted"); MyClose(); } catch(SQLException el) { System.out.println(el); } } /*public static void main(String args[]) { //String MySqlString="update StudentsTable set StuName='liuming' where StuID=3"; String MySqlString="select * from StudentsTable"; //String MySqlString="delete StudentsTable where StuName='liuming'"; //String MySqlString="insert into StudentsTable values(6,'关羽','男')"; //UpdateData(MySqlString); SelectData(MySqlString); //InsertData(MySqlString); //DeleteData(MySqlString); }*/}
上一篇:Ajax应用(应用)FAQ
下一篇:JavaScript得到客户端的信息
登录 注册