Exemple #1
0
        private Connection GetConnectionInternal()
        {
            // load driver class
            string driverClassName = properties.GetProperty("driverClassName");

            try {
                ClassLoader cl = Thread.CurrentThread().ContextClassLoader;
                Type.ForName(driverClassName, true, cl);
            } catch (ClassNotFoundException ex) {
                throw new RuntimeException("Error loading driver class '" + driverClassName + '\'', ex);
            } catch (RuntimeException ex) {
                throw new RuntimeException("Error loading driver class '" + driverClassName + '\'', ex);
            }

            // use driver manager to get a connection
            Connection connection;
            string     url  = properties.GetProperty("url");
            string     user = properties.GetProperty("username");
            string     pwd  = properties.GetProperty("password");

            try {
                connection = DriverManager.GetConnection(url, user, pwd);
            } catch (SQLException ex) {
                string detail = "SQLException: " + ex.Message +
                                " SQLState: " + ex.SQLState +
                                " VendorError: " + ex.ErrorCode;

                throw new RuntimeException("Error obtaining database connection using url '" + url +
                                           "' with detail " + detail
                                           , ex);
            }

            return(connection);
        }
 /// <exception cref="Java.Sql.SQLException"/>
 internal static void Connect()
 {
     if (connection == null)
     {
         System.Diagnostics.Debug.Assert(googleNgram_dbname != null, "set googleNgram_dbname variable through the properties file");
         connection = DriverManager.GetConnection("jdbc:postgresql://" + googleNgram_hostname + "/" + googleNgram_dbname, googleNgram_username, string.Empty);
     }
 }
Exemple #3
0
 /// <summary>Returns a connection object o the DB</summary>
 /// <exception cref="System.TypeLoadException"></exception>
 /// <exception cref="Java.Sql.SQLException"></exception>
 public virtual Connection GetConnection()
 {
     Sharpen.Runtime.GetType(conf.Get(Org.Apache.Hadoop.Mapreduce.Lib.DB.DBConfiguration
                                      .DriverClassProperty));
     if (conf.Get(Org.Apache.Hadoop.Mapreduce.Lib.DB.DBConfiguration.UsernameProperty)
         == null)
     {
         return(DriverManager.GetConnection(conf.Get(Org.Apache.Hadoop.Mapreduce.Lib.DB.DBConfiguration
                                                     .UrlProperty)));
     }
     else
     {
         return(DriverManager.GetConnection(conf.Get(Org.Apache.Hadoop.Mapreduce.Lib.DB.DBConfiguration
                                                     .UrlProperty), conf.Get(Org.Apache.Hadoop.Mapreduce.Lib.DB.DBConfiguration.UsernameProperty
                                                                             ), conf.Get(Org.Apache.Hadoop.Mapreduce.Lib.DB.DBConfiguration.PasswordProperty)
                                            ));
     }
 }
Exemple #4
0
 /// <exception cref="System.Exception"/>
 private void CreateConnection(string driverClassName, string url)
 {
     Sharpen.Runtime.GetType(driverClassName);
     connection = DriverManager.GetConnection(url);
     connection.SetAutoCommit(false);
 }
Exemple #5
0
        public void Connect()
        {
            Java.Sql.IConnection con = null;
            try {
                var    driver     = new Net.Sourceforge.Jtds.Jdbc.Driver();
                String username   = "******";
                String password   = "******";
                String address    = "192.168.1.101";
                String port       = "1433";
                String database   = "Database";
                String connString = String.Format("jdbc:jtds:sqlserver://{0}:{1}/{2};user={3};password={4}",
                                                  address, port, database, username, password);
                con = DriverManager.GetConnection(connString, username, password);
                IPreparedStatement stmt = null;
                try {
                    //Prepared statement
                    stmt = con.PrepareStatement("SELECT * FROM Users WHERE Id = ? AND Name = ?");
                    stmt.SetLong(1, 1);
                    stmt.SetString(2, "John Doe");
                    stmt.Execute();

                    RunOnUiThread(() => Toast.MakeText(this, "SUCCESS!", ToastLength.Short).Show());

                    IResultSet rs = stmt.ResultSet;

                    IResultSetMetaData rsmd = rs.MetaData;
                    PrintColumnTypes.PrintColTypes(rsmd);
                    Console.WriteLine("");

                    int numberOfColumns = rsmd.ColumnCount;

                    for (int i = 1; i <= numberOfColumns; i++)
                    {
                        if (i > 1)
                        {
                            Console.Write(",  ");
                        }
                        String columnName = rsmd.GetColumnName(i);
                        Console.Write(columnName);
                    }
                    Console.WriteLine("");
                    while (rs.Next())
                    {
                        for (int i = 1; i <= numberOfColumns; i++)
                        {
                            if (i > 1)
                            {
                                Console.Write(",  ");
                            }
                            String columnValue = rs.GetString(i);
                            Console.Write(columnValue);
                        }
                        Console.WriteLine("");
                    }

                    stmt.Close();
                    con.Close();
                } catch (Exception e) {
                    RunOnUiThread(() => Toast.MakeText(this, e.Message, ToastLength.Long).Show());
                    Console.WriteLine(e.StackTrace);
                    stmt.Close();
                }
                con.Close();
            } catch (Exception e) {
                Console.WriteLine(e.StackTrace);
                RunOnUiThread(() => Toast.MakeText(this, e.Message, ToastLength.Long).Show());
            }
            RunOnUiThread(() => _button.Enabled = true);
        }
Exemple #6
0
 /// <exception cref="Java.Sql.SQLException"/>
 public static IConnection GetConnection()
 {
     //System.out.println("username is " + dbusername + " and location is " + dbLocation);
     return(DriverManager.GetConnection(dbLocation + "?host=" + host + "&user="******"&password="******"&characterEncoding=utf-8&" + "useUnicode=true"));
 }
Exemple #7
0
        public static void Main(string[] args)
        {
            string db_file_name_prefix = args.Length > 0 && !"-info".Equals(args[0].ToLower()) ? args[0] : "test_db";

            //for now just terminate if we're invoked with the -info option
            if (args.Length > 0 && "-info".Equals(args[0].ToLower()))
            {
                return;
            }

            //the database connection
            Connection conn = null;

            Console.WriteLine("Success1!");

            IJvmLoader loader = JvmLoader.GetJvmLoader(true, true, TraceFacility.TraceJvm, TraceLevel.TraceErrors);

            //loader.JvmPath = @"jvm.dll";
            try
            {
                loader.JvmPath = @"C:\Program Files\Java\jdk1.8.0_161\jre\bin\server\jvm.dll";
                //loader.AppendBootClassPath = "hsqldb.jar";
                loader.AppendToClassPath("hsqldb.jar");
                //Append your db jar:
                // loader.AppendToClassPath("MyHSQLDB.jar");
                //Append any other Java jars needed eg:
                loader.AppendToClassPath("castor-0.9.3.21.jar");
                IJvm jvm = loader.Load();
                if (jvm != null)
                {
                    Console.WriteLine("Success2!");
                    // put the hsqldb classes (driver) on the bootclasspath to work around a
                    // limitation in the DriverManager when invoked from .NET
                    //loader.AppendBootClassPath = "../lib/hsqldb.jar;../../lib/hsqldb.jar";


                    Console.WriteLine("Success2.1!");
                    Class.ForName("java.lang.String");
                    Console.WriteLine("Success2.5!");

                    Console.WriteLine("Success3!");
                    // make the JDBC driver available by preloading it
                    Class.ForName("org.hsqldb.jdbcDriver");
                    Console.WriteLine("Success4!");
                    // connect to the database.   This will load the db files and start the
                    // database if it is not alread running.
                    // db_file_name_prefix is used to open or create files that hold the state
                    // of the db.
                    // It can contain directory names relative to the
                    // current working directory



                    conn = DriverManager.GetConnection("jdbc:hsqldb:" + db_file_name_prefix,
                                                       "sa",                      // username
                                                       "");
                    Console.WriteLine("Success5!");
                    try
                    {
                        //Uncomment this first time run:
                        //Update(conn, "CREATE TABLE sample_table ( id INTEGER IDENTITY, str_col VARCHAR(256), num_col INTEGER)");

                        //// add some rows - will create duplicates if run more then once
                        //// the id column is automatically generated
                        Update(conn, "INSERT INTO sample_table(str_col,num_col) VALUES('Ford', 100)");
                        Update(conn, "INSERT INTO sample_table(str_col,num_col) VALUES('Toyota', 200)");
                        Update(conn, "INSERT INTO sample_table(str_col,num_col) VALUES('Honda', 300)");
                        Update(conn, "INSERT INTO sample_table(str_col,num_col) VALUES('GM', 400)");
                        Update(conn, "INSERT INTO sample_table(str_col,num_col) VALUES('BMW', 80)");
                        Update(conn, "INSERT INTO sample_table(str_col,num_col) VALUES('Mercedes-Benz', 60)");
                        Update(conn, "INSERT INTO sample_table(str_col,num_col) VALUES('VW', 800)");
                    }
                    catch (SQLException sqle)
                    {
                        Console.WriteLine(sqle.Message);
                    }
                    catch (Throwable it)
                    {
                        Console.WriteLine(it.StackTrace);
                    }
                    catch (System.Exception ie)
                    {
                        Console.WriteLine(ie.ToString());
                    }
                    Console.WriteLine("Success6!");
                    try
                    {
                        Console.WriteLine("---------------");

                        // do a query
                        Query(conn, "SELECT * FROM sample_table WHERE num_col < 250");


                        Console.WriteLine("---------------");

                        // do another query
                        //Query(conn, "SELECT str_col FROM sample_table WHERE num_col >= 100");

                        Console.WriteLine("---------------");

                        conn.Close();
                    }
                    catch (Throwable t)
                    {
                        Console.WriteLine("Caught throwable of type {0}", t.GetClass().GetName());
                        Console.WriteLine(t.ToString());
                        return;
                    }
                    catch (JuggerNETFrameworkException jnfe)
                    {
                        Console.WriteLine("Caught framework exception");
                        Console.WriteLine(jnfe.Message);
                        Console.WriteLine(jnfe.StackTrace);
                        return;
                    }
                    Console.WriteLine("Success7!");
                }
                else
                {
                    Console.WriteLine("No Jvm!");
                }
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("Done!");
        }