i want to insert double dimensional array into ms-access dynamically in java ..
here is my code..
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver " +
"(*.mdb, *.accdb)};DBQ=C:\\Documents and Settings\\ANIL KUMAR\\Desktop\\hyperdata.mdb";
con = DriverManager.getConnection(url);
System.out.println("Connected!");
}
catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
}
catch (Exception e) {
e.printStackTrace();
}
if i have a string array with two columns:
String[][] a = new String[10][2];
PreparedStatement pst = con.prepareStatement("INSERT INTO sap_details VALUES (?,?)");
for (int i = 0; i < 10; i++) {
pst.setString(1, a[i][0]);
pst.setString(2, a[i][1]);
pst.addBatch();
}
pst.executeBatch();
what if have a have string array with n columns and n rows?
how to insert string array a[n][n]?
Have an inner for loop
for (int i=0; i<a.length; i++) {
for (int j=0; j<a[i].length; j++) {
System.out.print(a[i][j]);
}
}
You can also use enhanced loop as below
for (String[] array : a) {
for (String s : array) {
System.out.println(s);
}
}
Related
Hi i have a jtable in witch i want to save and load from a txtfile.
Now i have achived that but now im wondering in witch manner could i import the data into a already created arraylist.
Import Code
String filePath = "C:\\Users\\ellim\\Desktop\\TitanPanel\\TitanPanel.txt";
File file = new File(filePath);
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
DefaultTableModel model = (DefaultTableModel) tbl_panels.getModel();
Object[] lines = br.lines().toArray();
for (int i = 0; i < lines.length; i++) {
String[] row = lines[i].toString().split(" ");
model.addRow(row);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(PanelTable.class.getName()).log(Level.SEVERE, null, ex);
}
Export Code
String filePath = "C:\\Users\\ellim\\Desktop\\TitanPanel\\TitanPanel.txt";
File file = new File(filePath);
try {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < tbl_panels.getRowCount(); i++) {
for (int j = 0; j < tbl_panels.getColumnCount(); j++) {
bw.write(tbl_panels.getValueAt(i, j).toString() + " ");
}
bw.newLine();
}
bw.close();
fw.close();
} catch (IOException ex) {
Logger.getLogger(PanelTable.class.getName()).log(Level.SEVERE, null, ex);
}
}
Arraylist i want to save into
ArrayList documentos;
Here is code for ArrayList export
ArrayList<String> documentos = new ArrayList<String>();
// here i am using , as separator
ArrayListExporter exporter = new ArrayListExporter ( documentos , "," );
exporter.export( tbl_panels );
public interface TableDataExporter {
public void export( JTable table );
}
public class FileExporter implements TableDataExporter {
private String fileName;
private String separator ;
public FileExporter( String fileName,String separator ) {
this.fileName=fileName;
this.separator=separator;
}
public void export( JTable table ) {
File file = new File(fileName);
try {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < tbl_panels.getRowCount(); i++) {
for (int j = 0; j < tbl_panels.getColumnCount(); j++) {
if( j != 0 ) {
bw.write( separator );
}
bw.write(tbl_panels.getValueAt(i, j).toString() );
}
bw.newLine();
}
bw.close();
fw.close();
} catch (IOException ex) {
Logger.getLogger(PanelTable.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class ArrayListExporter implements TableDataExporter {
private List<String> list;
private String separator;
public FileExporter( List<String> list,String separator ) {
this.list=list;
this.separator=separator;
}
public void export( JTable table ) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < tbl_panels.getRowCount(); i++) {
builder.delete(0,builder.length());
for (int j = 0; j < tbl_panels.getColumnCount(); j++) {
if( j != 0 ) {
builder.append( separator );
}
Object obj = tbl_panels.getValueAt(i, j);
builder.append( obj == null ? "null" : obj.toString() );
}
}
}
}
i try to write on label an array with methods. When i try to put values of array it writes system int 32 on label, here is the code and how can put values on label
p.s. sorry about bad English
enter code here void arr_5( int[] mas5)
{
for (int i=0, j=5; i<10; i++, j+=5)
{
mas5[i] = j;
}
}
private void button1_Click(object sender, EventArgs e)
{
int[] a = new int[10];
arr_5 (a);
label1.Text += a.ToString() + " ";
}
That's because an array contains more than one value and in order to show it you could do something like:
int[] arr_5( int[] mas5)
{
for (int i=0, j=5; i<10; i++, j+=5)
{
mas5[i] = j;
}
return mas5;
}
private void button1_Click(object sender, EventArgs e)
{
int[] a = new int[10];
arr_5 (a);
String label="";
for(int i=0; i<a.length; a++)
{
label= label + a +" ";
}
label1.Text =label;
}
Please be aware that i've changed your arr_5 to return the newly created array.
So right now I'm making a mod in Minecraft where it takes everyones username from a server and adds it to a txt file, it works but the the problem is I don't want to duplicate the names when I use the command again. Nothing has worked so far. How would I check if the txt already contains the username, don't add it again? Thank you. Again, I need it to before writing another name to the list, check the txt file if it already contains the name, if so don't add it.
[code]
for (int i = 0; i < minecraft.thePlayer.sendQueue.playerInfoList.size(); i++) {
List playerList = minecraft.thePlayer.sendQueue.playerInfoList;
GuiPlayerInfo playerInfo = (GuiPlayerInfo) playerList.get(i);
String playerName = StringUtils.stripControlCodes(playerInfo.name);
try {
fileWriter = new FileWriter(GameDirectory() + "\\scraped.txt", true);
bufferedReader = new BufferedReader(new FileReader(GameDirectory() + "\\scraped.txt"));
lineNumberReader = new LineNumberReader(new FileReader(GameDirectory() + "\\scraped.txt"));
} catch (IOException e) {
e.printStackTrace();
}
printWriter = new PrintWriter(fileWriter);
try {
fileWriter.write(playerName + "\r\n");
lineNumberReader.skip(Long.MAX_VALUE);
} catch (IOException e) {
e.printStackTrace();
}
printWriter.flush();
}
addMessage("Scraped " + lineNumberReader.getLineNumber() + " usernames!");
}
[/code]
I've tried this too but with this it doesn't even write anymore.
[code]
List playerList = minecraft.thePlayer.sendQueue.playerInfoList;
for (int i = 0; i < minecraft.thePlayer.sendQueue.playerInfoList.size(); i++) {
GuiPlayerInfo playerInfo = (GuiPlayerInfo) playerList.get(i);
String playerName = StringUtils.stripControlCodes(playerInfo.name);
String lines;
try {
if ((lines = bufferedReader.readLine()) != null) {
if (!lines.contains(playerName)) {
bufferedWriter.write(playerName);
bufferedWriter.newLine();
bufferedWriter.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
int linenumber = 0;
try {
while (lineNumberReader.readLine() != null) {
linenumber++;
}
} catch (IOException e) {
e.printStackTrace();
}
[/code]
I am having trouble creating a method to import data from a txt file into a 1d string array and a 2d double array.
Here is what I have thus far:
public static void main(String[] args){
String[] productName = new String[100];
double[][] sales = new double[100][5];
initializeArrays(productName, sales);
....}
public static void initializeArrays(String a[], double b[][]){
try
{
String output = "";
File inputFile = new File("c:/temp/salesdata.txt");
if (inputFile.exists())
{
Scanner scanner = new Scanner(inputFile);
while (scanner.hasNext())
{
for (i=1;i<6;i++)
{
b[a.length][i]=scanner.nextDouble();
}
rowCount += 1;
}
}
}
catch (FileNotFoundException e)
{
System.out.println("Error reading file: ");
}
Try this code:
public static void initializeArrays(String a[], double b[][]){
try
{
String output = "";
File inputFile = new File("c:/temp/salesdata.txt");
int counter = 0;
if (inputFile.exists())
{
Scanner scanner = new Scanner(inputFile);
while (scanner.hasNext())
{
for (i=1;i<6;i++)
{
b[counter][i]=scanner.nextDouble();
}
counter++;
rowCount += 1;
}
}
}
catch (FileNotFoundException e)
{
System.out.println("Error reading file: ");
}
since I am beginner, Jtable concept has made my brain rack a lot. with difficult i have maganaged to read a database and add it to jtable CODE given below. but am stuck as how to ADD A JCHECKBOX IN THE LAST COLUMN OF THE SAME TABLE.
public class ttt extends JFrame{
ResultSet rs;
int colcount;
String[] headers;
Connection con;
Statement st;
ttt(){
final Vector columnNames = new Vector();
final Vector data = new Vector();
JPanel panel=new JPanel();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
// Establish a connection
con= DriverManager.getConnection
("jdbc:odbc:ysr");
System.out.println("Database connecteddddd");
// Create a statement
st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT
Block_Name,Panchayat_Name,Village_Name," +
" Habitation_Name,Scheme_Name,Sanction_Amount FROM
ysr2011 where Habitation_Name= '10th mile' ");
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
}
catch(Exception e){}
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
add(panel);
}
public static void main(String arg[])
{
try
{
ttt frame=new ttt();
frame.setSize(550,200);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}
ANY HELP IS REALLY A WELCOME GESTURE. THANKING IN ADVANCE.
You would add the check box here:
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
columnNames.addElement("Check Box");
while (rs.next()) {
Vector row = new Vector(columns + 1);
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
row.addElement(new JCheckBox());
data.addElement( row );
}
Use your own column title. You're also going to have to define the check boxes more completely because eventually you're going to add tests for the isSelected method.