`

lotteryutil

db 
阅读更多
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.table.AbstractTableModel;


public class DBUtils {
public void insert(AbstractTableModel tableModel){
int row = tableModel.getRowCount();
int col = tableModel.getColumnCount();
for(int i=0;i<row;i++){
if(isRecordExist((String)tableModel.getValueAt(i, 0))){
continue;
}
StringBuffer record = new StringBuffer("insert into pai3(");
for(int j=0;j<col;j++){
record.append("\'");
record.append(tableModel.getValueAt(i, j));
record.append("\',");
}
String sql = record.substring(0,record.length()-1)+")";
// execute(sql);
System.out.println(sql);
}
}

public boolean isRecordExist(String id){
String statement = "select * from pai3 where id =" + id;
ResultSet resultSet = execute(statement);

String Id = null;
try{
resultSet.next();
Id = resultSet.getString(1);
}catch (Exception e) {
e.printStackTrace();
}

if(Id != null)
return true;
else
return false;
}

public static Connection oracleConnect(String database,String name, String password){
Connection connection = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(database, name, password);
}catch (Exception e) {
e.printStackTrace();
}
return connection;
}

public static ResultSet execute(String sql){
ResultSet resultSet = null;
try{
Statement statement = oracleConnect("jdbc:oracle:thin:@10.43.104.133:1521:lottery", "system", "lottery").createStatement();
resultSet = statement.executeQuery(sql);
}catch (Exception e) {
e.printStackTrace();
}
return resultSet;
}

public static void main(String args []){
DBUtils dbUtils = new DBUtils();
dbUtils.insert(new RackModel());
// if(dbUtils.isRecordExist("11182")){
// System.out.println(" is exist");
// }else{
// System.out.println(" is not exist");
// }
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics