How to get stored procedure code from the database? [duplicate] - sql-server

When I try to retrieve stored procedures from an SQL Server database using the SchemaCrawler API, I get this error:
12:28:07.427 [main] INFO schemacrawler.crawl.SchemaCrawler - Retrieving routines
12:28:07.767 [main] WARN schemacrawler.crawl.RoutineRetriever - JDBC driver does not support retrieving functions
java.lang.AbstractMethodError: null
at net.sourceforge.jtds.jdbc.JtdsDatabaseMetaData.getFunctions(JtdsDatabaseMetaData.java:3570) ~[jtds-1.3.1.jar:1.3.1]
at schemacrawler.crawl.RoutineRetriever.retrieveFunctions(RoutineRetriever.java:175) ~[schemacrawler-14.02.02.jar:na]
at schemacrawler.crawl.SchemaCrawler.crawlRoutines(SchemaCrawler.java:214) [schemacrawler-14.02.02.jar:na]
at schemacrawler.crawl.SchemaCrawler.crawl(SchemaCrawler.java:564) [schemacrawler-14.02.02.jar:na]
at schemacrawler.utility.SchemaCrawlerUtility.getCatalog(SchemaCrawlerUtility.java:49) [schemacrawler-14.02.02.jar:na]
at schemacrawler.utility.SchemaCrawlerUtility.getCatalog(SchemaCrawlerUtility.java:57) [schemacrawler-14.02.02.jar:na]
at com.expedia.cgs.db.ExportScripts.main(ExportScripts.java:41) [classes/:na]
The jtds driver supports DatabaseMetaData.getProcedures() but not DatabaseMetaData.getFunctions(). Is there a workaround?
UPDATE: Here is my code:
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.slf4j.bridge.SLF4JBridgeHandler;
import schemacrawler.schema.Catalog;
import schemacrawler.schema.Column;
import schemacrawler.schema.Routine;
import schemacrawler.schema.Schema;
import schemacrawler.schema.Table;
import schemacrawler.schema.View;
import schemacrawler.schemacrawler.DatabaseConnectionOptions;
import schemacrawler.schemacrawler.RegularExpressionInclusionRule;
import schemacrawler.schemacrawler.SchemaCrawlerException;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.schemacrawler.SchemaInfoLevelBuilder;
import schemacrawler.utility.SchemaCrawlerUtility;
public class ExportScripts {
public static void main(String[] args) throws SchemaCrawlerException, SQLException {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
// Create a database connection
final DataSource dataSource = new DatabaseConnectionOptions("jdbc:sqlserver://myDatabase;appName=SchemaCrawler;useCursors=true");
final Connection connection = dataSource.getConnection("username", "password");
// Create the options
final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
// Set what details are required in the schema - this affects the
// time taken to crawl the schema
options.setSchemaInfoLevel(SchemaInfoLevelBuilder.standard());
options.setRoutineInclusionRule(new RegularExpressionInclusionRule("ContentGeneration\\.dbo.*"));
options.setSchemaInclusionRule(new RegularExpressionInclusionRule("ContentGeneration\\.dbo.*"));
// Get the schema definition
final Catalog catalog = SchemaCrawlerUtility.getCatalog(connection,
options);
for (final Schema schema : catalog.getSchemas()) {
System.out.println(schema);
for (final Routine routine : catalog.getRoutines()) {
System.out.println("r--> " + routine);
System.out.println("definition: " + routine.getDefinition());
}
for (final Table table : catalog.getTables(schema)) {
System.out.print("o--> " + table);
if (table instanceof View) {
System.out.println(" (VIEW)");
} else {
System.out.println();
}
for (final Column column : table.getColumns()) {
System.out.println(" o--> " + column + " ("
+ column.getColumnDataType() + ")");
}
}
}
}
}

Here is how to get full SchemaCrawler support for Microsoft SQL Server.
Include the SchemaCrawler jar that has Microsoft SQL Server support, in your Maven project.
<dependency>
<groupId>us.fatehi</groupId>
<artifactId>schemacrawler-sqlserver</artifactId>
<version>14.02.02</version>
</dependency>
Use code similar that this below.
final DatabaseSystemConnector dbSystemConnector = new SqlServerDatabaseConnector().getDatabaseSystemConnector();
final DatabaseSpecificOverrideOptions databaseSpecificOverrideOptions = dbSystemConnector.getDatabaseSpecificOverrideOptionsBuilder().toOptions();
final SchemaCrawlerOptions schemaCrawlerOptions = new SchemaCrawlerOptions();
schemaCrawlerOptions.setSchemaInfoLevel(InfoLevel.maximum.buildSchemaInfoLevel());
final Catalog catalog = SchemaCrawlerUtility.getCatalog(getConnection(), databaseSpecificOverrideOptions, schemaCrawlerOptions);
Loop over the routines, and use Routine.getDefinition() to get the definitions.
Sualeh Fatehi, SchemaCrawler

Related

How do I connect Apache Flink to TDengine database?

I want to connect Flink to TDengine database. I configured it successfully.
However, when I restart a job, it is said:
Native Library /usr/local/taos/driver/libtaos.so.2.0.8.2 already loaded in another classloader
Does anyone successfully connect Flink with TDengine? Would you like to share some experience, thank you.
here is the sample code from this post: https://www.taosdata.com/blog/2022/05/30/9165.html
package com.taosdata.java;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SaveMode;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.jdbc.JdbcDialect;
import org.apache.spark.sql.jdbc.JdbcDialects;
public class SparkTest{
public static void main(String[] args) {
// 数据库配置
String url = "jdbc:TAOS://u05:6030/tt?user=root&password=taosdata";
String driver = "com.taosdata.jdbc.TSDBDriver";
String dbtable = "t1";
SparkSession sparkSession = SparkSession.builder()
.appName("DataSourceJDBC") // 设置应用名称
.master("local") // 本地单线程运行
.getOrCreate();
// 创建DataFrame
Dataset<Row> df = sparkSession
.read() // 返回一个DataFrameReader,可用于将非流数据作为DataFrame读取
.format("jdbc") // JDBC数据源
.option("url", url)
.option("driver", driver)
.option("query", "select * from tt.meters limit 100") // 二选一,sql语句或者表
.load();
// 将DataFrame的内容显示
df.show();
df.write() // 返回一个DataFrameWriter,可用于将DataFrame写入外部存储系统
.format("jdbc") // JDBC数据源
.mode(SaveMode.Append) // 如果第一次生成了,后续会追加
.option("url", url)
.option("driver", driver)
.option("dbtable", "test.meters") // 表名
.save();
sparkSession.stop();
}
}

I am getting error Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

package GoldenGate;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLConnector
{
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
//Load mysql jdbc driver
Class.forName("com.mysql.jdbc.Driver");
//Create Connection to DB
Connection con = DriverManager.getConnection("jdbc:mysql://10.1.0.22:3310/test_db","rax","rax#123");
//Create Statement Object
Statement stmt = con.createStatement();
// Execute the SQL Query. Store results in ResultSet
ResultSet rs= stmt.executeQuery("select * from users;");
// While Loop to iterate through all data and print results
while (rs.next())
{
String myName = rs.getString(1);
String myAge = rs.getString(2);
System. out.println(myName+" "+myAge);
}
// closing DB Connection
con.close();
}
}
how to connect to database which have private IP.
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Loading SSIS package and it's connection managers in a WPF application

I created an integration service project with three connection managers (which are defined in project level), now I have an SSIS package (.dtsx file) and I want to use it in my app.
So I use Microsoft.SqlServer.Dts namespace as below:
string pkgLocation;
Package pkg;
Microsoft.SqlServer.Dts.Runtime.Application app;
DTSExecResult pkgResults;
pkgLocation = #"D:\My Job\Tadbirgaran\Integration Services Project\MyPackage.dtsx";
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadPackage(pkgLocation, null);
pkgResults = pkg.Execute();
but this code does not load project connection managers obviously, so is there any way that I can load connection manager files programmatically and add them to the package connection property? or should I define connections in my package in the integration service project?
You can use configurations to control the connection properties programmatically. Here is an example.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Tasks.BulkInsertTask;
namespace configuration_API
{
class Program
{
static void Main(string[] args)
{
// Create a package and set two properties.
Package pkg = new Package();
pkg.EnableConfigurations = true;
pkg.ExportConfigurationFile(#"C:\conf.xml");
// Create a variable object and add it to the
// package Variables collection.
Variable varPkg = pkg.Variables.Add("var", false, "", 100);
varPkg.Value = 1;
string packagePathToVariable = varPkg.GetPackagePath();
// Create a configuration object and add it to the
// package configuration collection.
Configuration config = pkg.Configurations.Add();
// Set properties on the configuration object.
config.ConfigurationString = "conf.xml";
config.Description = "My configuration description";
config.ConfigurationType = DTSConfigurationType.ConfigFile;
config.PackagePath = packagePathToVariable;
// Save the package and its configuration.
Application app = new Application();
app.SaveToXml(#"c:\pkg.xml", pkg, null);
// Reload the package.
Package p1 = app.LoadPackage(#"c:\pkg.xml", null);
// Review the configuration information.
Configurations configs_After = pkg.Configurations;
foreach(Configuration confAfter in configs_After)
{
Console.WriteLine("ConfigurationString is {0}", confAfter.ConfigurationString);
Console.WriteLine("ConfigurationType is {0}", confAfter.ConfigurationType);
Console.WriteLine("CreationName is {0}", confAfter.CreationName);
Console.WriteLine("Description is {0}", confAfter.Description);
Console.WriteLine("Assigned ID is {0}", confAfter.ID);
Console.WriteLine("Name is {0}", confAfter.Name);
}
}
}
}
I ended up loading the project instead of package and setting connections from project to package as below:
private void SSISLoadData()
{
Project ssisProject = null;
DTSExecResult pkgResults;
try
{
ssisProject = Project.OpenProject(#"D:\My Job\TDB\Integration Services Project\bin\Development\Integration Services Project.ispac");
Package pkg = ssisProject.PackageItems[0].LoadPackage(null);
for (int i = 0; i < ssisProject.ConnectionManagerItems.Count; i++)
pkg.Connections.Join(ssisProject.ConnectionManagerItems[i].ConnectionManager);
pkg.Connections[0].ConnectionString = dataFolderPath + "\\*.csv";
pkg.Connections[1].ConnectionString = string.Format("Data Source =.; Initial Catalog = TDB; Provider = SQLNCLI11.1; Integrated Security = SSPI; Initial File Name = {0};", dbPath);
pkg.Connections[2].ConnectionString = string.Format("Data Source =.; Initial Catalog = TDBRawData; Provider = SQLNCLI11.1; Integrated Security = SSPI; Initial File Name = {0}\\TDBRawData.mdf;", Environment.CurrentDirectory);
pkgResults = pkg.Execute();
MessageBox.Show(pkgResults.ToString());
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
if (ssisProject != null)
ssisProject.Dispose();
}
}
I don't know if there is a better solution for this problem, but it works.

Get row of database(phpmyadmin) datatable using selenium webdriver

I am new to selenium driver . How to get data of row from database table. I tried this code where my database name is "demodatabase" , table name is "state" and row id is 2
package com.db;
import java.sql.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbConnection {
public static void main(String[] args) throws ClassNotFoundException, SQLException{
/*------connection url----*/
String dbUrl= "jdbc:mysql://localhost/demodatabase";
/*------dbUsername----*/
String dbUsername= "root";
/*------dbPassword----*/
String dbPassword= "";
/*------db query---*/
String query= "select * from states where id=2";
/*-----load Mysql jdbc driver------*/
Class.forName("com.mysql.jdbc.Driver");
/*----Get connection to DB*/
Connection con = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
//create statement object
Statement stmt = con.createStatement();
//send sql query to database
ResultSet rs= stmt.executeQuery(query);
// while loop to get ResultSet all rows data
while(rs.next()){
String state =rs.getString("2");
System.out.println(state);
}
//Close db connection
con.close();
}
}
Database table Screenshot
http://i.stack.imgur.com/dcaGc.png
Using above code and database screenshoot .I am getting data of id=2 and column 2 i.e Jharkhand only but I want all data of row 2
In the above code you have not got the column values of the table's second row
Kindly refer the below code
package com.db;
import java.sql.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbConnection {
public static void main(String[] args) throws ClassNotFoundException, SQLException{
/*------connection url----*/
String dbUrl= "jdbc:mysql://localhost/demodatabase";
/*------dbUsername----*/
String dbUsername= "root";
/*------dbPassword----*/
String dbPassword= "";
/*------db query---*/
String query= "select * from states where id=2";
/*-----load Mysql jdbc driver------*/
Class.forName("com.mysql.jdbc.Driver");
/*----Get connection to DB*/
Connection con = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
//create statement object
Statement stmt = con.createStatement();
//send sql query to database
ResultSet rs= stmt.executeQuery(query);
// while loop to get ResultSet all rows data
while(rs.next()){
//Store columns state,country,created,modified as separate strings
//(pls chk spellings of column name and also datatypes of the column if it is int change it to (rs.getInt) before running)
String state =rs.getString("name");
String country =rs.getString("country_id");
String created_DATE= rs.getString("created");
String modified_DATE=rs.getString("modified");
System.out.println(state);
System.out.println(country);
System.out.println(created_DATE);
System.out.println(modified_DATE);
}
//Close db connection
con.close();
}
}
Hope this helps you...Kindly get back if it is not working

Display SQL Server table values using jdbc

Below is my code to display SQL Server values using jdbc. I have a problem. it says it is connected, but cannot display the values from the table specified. it says, invalid object name .
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DateServer{
public static void main(String[] argv) throws Exception {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String user = "abc";
String pass = "def";
Class.forName(driver).newInstance();
Connection con = DriverManager.getConnection("jdbc:sqlserver://<hostname:port;database name>, user, pass);
System.out.println("connected");
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT CommentID FROM Comment");
while (res.next()) {
int i = res.getInt("CommentID");
System.out.println(i);
}
con.close();
}
}
Below is the error:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'Comment'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:775)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:676)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:611)
at DateServer.main(DateServer.java:19)
Comment is a reserved word, therefor you need to quote it in the select statement:
st.executeQuery("SELECT CommentID FROM \"Comment\"");

Resources