test.NewClass.java Source code

Java tutorial

Introduction

Here is the source code for test.NewClass.java

Source

package test;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author user
 */
public class NewClass {

    public static void main(String[] args) throws JSONException {

        String db_url = "jdbc:jtds:sqlserver://w2ksa.cs.cityu.edu.hk:1433/aiad001_db;";
        String user = "aiad001";
        String db_user = user;
        String pwd = "aiad001";
        String db_password = pwd;
        String str_q = "SELECT * FROM \"User\"";
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            Connection conn = DriverManager.getConnection(db_url, user, pwd);
            Statement statmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet rs = statmt.executeQuery(str_q);
            ResultSetMetaData rsmd = rs.getMetaData();
            int numOfColumns = rsmd.getColumnCount();
            for (int i = 1; i <= numOfColumns; i++) {
                System.out.printf("%s ", rsmd.getColumnName(i));
            }
            System.out.println("");
            while (rs.next()) {
                System.out.printf("{%s %20s %s %s}\n", rs.getString(1), rs.getString(2), rs.getString(3),
                        rs.getString(4));
                if (rs.getString(1).equals("1"))
                    rs.updateString(4, "abcd1111");
                System.out.println(rsmd);
            }

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}