?? connect.java
字號:
package mainface;
import java.sql.*;
public class Connect {//數據庫連接部分
String[] strin=null;
Connection conn;
int i=0;
int k=0;
String string[];
String str[][]=new String[50][7];;
public Connection getTable(){//數據庫的連接;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("數據庫驅動程序加載驅動成功!");
}catch(Exception s){
System.out.println("數據庫加載驅動失敗!");
s.printStackTrace();
}
try{
conn=DriverManager.getConnection("jdbc:odbc:student","","");
System.out.println("數據庫連接成功!");
}catch(Exception a){
System.out.println("數據庫連接不成功!");
}
return conn;
}
public String[] Quiry(String args,String args1){//數據庫的查詢;
Connection conn=getTable();
try{
strin=null;
PreparedStatement state=conn.prepareStatement("select*from chengjibiao where "+args+" = ?");
state.setString(1,args1);
ResultSet result=state.executeQuery();
while(result.next()){
String id=result.getString("ID");
String name=result.getString("name");
String xuehao=result.getString("xuehao");
String birthday=result.getString("birthday");
String math=result.getString("math");
String english=result.getString("english");
String Cyuyan=result.getString ("Cyuyan");
strin=new String[]{id,xuehao,name,birthday,math,english,Cyuyan};
}
}catch(Exception t){
t.printStackTrace();
}
return strin;
}
public String[][] ScanL(){
k=0;
Connection con=getTable();
PreparedStatement state=con.prepareStatement("select*from chengjibiao ");
ResultSet result=state.executeQuery();
while(result.next()){
k++;
String id=result.getString("ID");
String name=result.getString("name");
String xuehao=result.getString("xuehao");
String birthday=result.getString("birthday");
String math=result.getString("math");
String english=result.getString("english");
String Cyuyan=result.getString ("Cyuyan");
string=new String[]{id,xuehao,name,birthday,math,english,Cyuyan};
for(int j=0;j<7;j++)
str[i][j]=string[j];
System.out.println(str[i][2]);
++i;
}
for(int j=0;j<i;j++)
{for(int a=0;a<7;a++)
System.out.print(str[j][a]+" ");
System.out.println();}
}catch(Exception a){
a.printStackTrace();
}
return str;
}
public int Cont(){
return k;
}
public boolean InsertQ(String s[]){//數據插入;
Connection con=getTable();
boolean n;
try{
PreparedStatement state=con.prepareStatement("insert into chengjibiao values(?,?,?,?,?,?,?)");
for(int j=0;j<7;j++)
state.setString(j+1,s[j] );
state.executeUpdate();
n=true;
}catch(Exception a){
a.printStackTrace();
n=false;
}
return n;
}
public boolean Delete(String a,String b){//數據刪除;
Connection con=getTable();
boolean n;
try{
PreparedStatement state=con.prepareStatement("delete from chengjibiao where "+a+ " = ?");
state.setString(1,b);
// state.setString(2,b);
System.out.println(a+b);
state.executeUpdate();
n=true;
}catch(Exception e){
n=false;
e.printStackTrace();
}
return n;
}
public boolean upDate(String a[]){
boolean h;
Connection con=getTable();
try{System.out.println();
PreparedStatement state=con.prepareStatement("update chengjibiao set "+a[0]+" = " +"'"+a[1]+"'"+" where "+a[2]+ "= "+"'"+a[3]+"'" );
state.executeUpdate();
if(a[1]==null||a[3]==null)
h=false;
else
h=true;
}catch(Exception e){
e.printStackTrace();
h= false;
}
return h;
}
public static void main(String args[]){
String b[]={"name","'蔡正武'","ID","'123'"};
new Connect().upDate(b);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -