JDBC connecting MySQL script need help using ScriptRunner - java

I have a Java application and i need it to connect with my MySQL database's SQL script using JDBC.
Here is my Java application:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package basic;
import basic.ScriptRunner;
import javax.swing.*;
import java.sql.*;
import java.io.*;
/**
*
* #author User
*/
public class javaconnect {
Connection conn = null;
public static Connection ConnectDb(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/new","login","pass");
/*As we are creating a connection on a local computer we will write the url as jdbc:mysql://localhost:3306 */
ScriptRunner runner = new ScriptRunner(conn, false, false);
runner.runScript(new BufferedReader(new FileReader("D://Java Lenti/EkonomiSoftware/src/basic/new.sql")));
return conn ;
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;}
}
}
The problem is that the Java application connects through MySQL Server, not through the SQL Script. I think the problem is at Connection parameters I gave. Can anyone guide me how to change the connection to make it connect to the SQL script not to the server?

It worked for me:
package com.spring.sample.controller;
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager
.getConnection(
"",
"", "");
ScriptRunner runner = new ScriptRunner(conn, false, false);
runner.runScript(new BufferedReader(new FileReader("new.sql")));
} catch (Exception e) {
e.printStackTrace();
}
}
}
new.sql
insert into hello(name) values ('test');
May be there're some mistakes at your sql file.

Related

How to resolve sqlite communication error in java IntelliJ

I'm trying to connect to a sql database with java but this isn't really working out. normally people say they have a user and password but I never created anything like that. I downloaded sqlite studio and you can pretty much just make a database without having to create some kind of account but when i'm trying to connect it gives me an error that it can't connect com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure.
How do I resolve this issue? This is the code I have right now:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MyJDBC {
public static void main(String[] args) {
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/JDBC-video");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from people");
while (resultSet.next()) {
System.out.println(resultSet.getString("firstname"));
}
}
catch (Exception e){
e.printStackTrace();
}
}
}

Java console application connection with mysql database

I am creating a java console application that connects to a MySQL database. I have outlined below what I've done but the connection is not being established:
Things I have done:
1) I have added the mysql-connector-java-5.1.36.jar to my project bulid path.
2) I have written the code that tries to establish a connection
Can anyone suggest what I'm doing wrong?
The code is given below:
import java.sql.Connection;
import java.sql.DriverManager;
public class main {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:1234/first","root","");
System.out.println("Success fully Connected");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}

How to preview jasper report using java?

i have created a report for my inventory control using IReport.then i found a code
to preview my report,this is that code,
package Report;
import com.mysql.jdbc.Connection;
import java.awt.Container;
import java.sql.Statement;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;
/**
*
* #author Hasindu
*/
public class ReportView extends JFrame
{
public ReportView(String fileName)
{
this(fileName, null);
}
public ReportView(String fileName, HashMap para)
{
super("ABC Solutions Employee/Project Management System (Report Viewer)");
try {
DB dba=new DB();
Connection con=DB.getConnection();
try
{
JasperPrint print = JasperFillManager.fillReport(fileName, para,con);
JRViewer viewer = new JRViewer(print);
Container c = getContentPane();
c.add(viewer);
}
catch (JRException j){
j.printStackTrace();
}
setBounds(10, 10, 900, 700);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
} catch (Exception ex) {
Logger.getLogger(ReportView.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
but my database connection code is returning a statement,this is my database connection code,
package Report;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
public class DB {
public static Statement getConnection()throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/report", "root", "123");
Statement st=con.createStatement();
return st;
}
}
Now i have two problems,
how to edit the report preview code to connect with my database connection code
i tested report preview code by creating this database connection code,
public static Connection getCon()throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/report", "root", "123");
}
but when i run this code it's appears a empty window,
please give a support
if i understand your question correctly ,you are returning a Statement but it require Connection .don't create a statement create a connection to your db and return that connection like following code example.
in reportview class
DB dba=new DB();
Connection con=DB.getCon();
db.class
package Report;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
public class DB {
public static Connection getCon() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/report", "root", "123");
return con;
}
}

Hive server not starting

Iam new to the hadoop ecosystem. Tried to access hive through jdbc. For that I have written the following code
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveConnection {
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000");
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery("select * from test.employees");
while (res.next()) {
System.out.println(res.getString(1));
} }}
and started the hiveserver2 (hive version 0.12 & hadoop version 1.1.2) through the terminal and im getting the status as "Starting HiveServer" . When i tried executing the above code from eclipse im no getting any error and any results neither(Got the same when i executed the executable "hiveserver" ).
Can any one help me out.
Thanks in advance.
Following ways are most really reason for your problem.
1.Is the hive-jdbc-*.Jar having classpath like this "org.apache.hadoop.hive.jdbc.HiveDriver" or "org.apache.hive.jdbc.HiveDriver"?
2.While connection,you need to pass the user name and password like this?
If you use hive server2 then you have to set the connection like below.
con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
Above ways are really helpful for you.
Thanks in advance.

Connecting Berkely DB in java through sqlitejdbc-v056.jar

i am connecting berkely database through sqlitejdbc-v056.jar & i got the following error and not performing insert ,update ,delete opration
java.sql.SQLException: database is locked
at org.sqlite.DB.throwex(DB.java:288)
at org.sqlite.NativeDB.prepare(Native Method)
at org.sqlite.DB.prepare(DB.java:114)
at org.sqlite.Stmt.executeUpdate(Stmt.java:102)
at testdb.TestProgram.main(TestProgram.java:37)
download jar from following link http://www.zentus.com/sqlitejdbc/
i am using following code for performing
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import org.sqlite.*;
/**
*
* #author dhananjay.joshi
*/
public class TestProgram
{
public static void main(String args[])
{
Connection con=null;
Statement smt=null;
ResultSet rs = null;
try
{
Class.forName("org.sqlite.JDBC");
System.out.print("Connnection req");
con = DriverManager.getConnection("jdbc:sqlite:C:\\Prog\\Emp.db");
smt = con.createStatement();
System.out.println("\n Connected");
String que = "insert into student values(2,'kiran')";
smt.executeUpdate("insert into student values(2,'kiran');");
System.out.print("Insert Data");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
SQLite is a light weight SQL database. Berkley-DB is not an SQL database. They both have nothing to do with each other.
If you want to read/write a Berkley DB file you need a jar file with the helper classes for Berkley-DB.
A google search for berkeley db java should help you.

Categories

Resources