How to set up online database for java netbeans application - java

i'm trying a online database connection for my java application, but it seems my database isn't connected well this is my java code :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("sql101.epizy.com/epiz_26206530_sekolah","LOGIN","PASSWORD");
String query = "select * from users where username=? and password=?";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, jTextField1.getText());
pst.setString(2, jPasswordField1.getText());
ResultSet rs = pst.executeQuery();
if(rs.next()) {
JOptionPane.showMessageDialog(null, "Login Berhasil");
jLabel7.setText(rs.getString(1));
welcome well = new welcome();
this.setVisible(false);
well.setVisible(true);
}
} catch (Exception e) {
}
}
i already input MySQL JDBC Driver library, by the way i'm using online database from https://infinityfree.net/

Related

mysql connector doesn't connect to my xxamp localhost

I am using netbeans 8.2 with xxamp. mysql connector.jar is 8.0.18.
It doesn't show any errors but whenever I press the button, the value from jUsername isn't inserted and instead the exception in the catch is shown. Please help.
private void StartButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String query = "INSERT INTO 'score' ('Name') VALUES (?)";
con = DriverManager.getConnection("jdbc:mysql://localhost/userscore", "root", "");
pst = con.prepareStatement(query);
pst.setString(1, jUsername.getText());
DifficultyPanel dpanel =new DifficultyPanel();
dpanel.setVisible(true);
this.dispose();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Please Enter Your Username To Continue");
}
}

JAVA: executing Catch not try

private void rtrBtnActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) depTbl.getModel();
try{
Class.forName("java.sql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test1","admin","root");
Statement stmt = con.createStatement();
String query = "SELECT * FROM dept;";
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
String dno = rs.getString("deptno");
String dName = rs.getString("dname");
String lc = rs.getString("loc");
model.addRow(new Object[] {dno,dName,lc});
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error In Connectivity");
}
}
Im trying to connect my JForm to the mysql database but not able to connect to the database, continuously executing catch statement "Error in Connectivity", please help how should i resolve this issue..............................................................................
Change the following line
Class.forName("java.sql.Driver");
to
Class.forName("com.mysql.jdbc.Driver");
in your code
This line of code is not correct - Class.forName("java.sql.Driver");
The java.sql.Driver is an interface. You need to provide the correct jdbc driver class for you appropriate db.
Such as for Oracle - Class.forName("oracle.jdbc.driver.OracleDriver");

Netbeans ucanaccess [duplicate]

This question already has an answer here:
invalid datetime format: insert date/time into Access from Java
(1 answer)
Closed 6 years ago.
conn = Connect.ConnectDB();
String sql = "insert into Ianouarios ("
+"id,"
+"Hmerominia,"
+"Agores,"
+"Pliromes,"
+"Eksoda,"
+ "Zhta,"
+"Metaforika,"
+"Pliromimetafo,"
+"Epitages,"
+"Xondriki,"
+"Noiki,"
+"Plirominoiki)"
+ "values("+a1.getText()+ ",'"+a2.getText()+"','"+a3.getText()+"','"+a4.getText()+"','"+a5.getText()+"','"+a6.getText()+"','"+a7.getText()+"','"+a8.getText()+"','"+a9.getText()+"','"+ a10.getText()+ "','"+a11.getText()+"','"+a12.getText()+"')" ;
try{
pst = conn.prepareStatement(sql);
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Saved");
//UpdateJTable();
//conn.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex);
I have an ms access database and i am updating it with the jdbc-odbc driver.
Can anyone show me how to do the same to work with ucanaccess driver?
I tried their ucanaccess page examples but no luck.
Update: I did this:
conn = Connect.ConnectDB();
try{
String sql = "INSERT INTO Synola(id,Hmerominia,Agores,Pliromes,Eksoda,Zhta,Metaforika,Pliromimetafo,Epitages,Xondriki,Noiki,Plirominoiki) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)" ;
/* SimpleDateFormat ddmmyyyyFormat = new SimpleDateFormat("dd/MM/yyyy");
Timestamp ddmmyyyy = new Timestamp(ddmmyyyyFormat.parse(a2.getText()).getTime()); */
// String s=ddmmyyyy.toString();
pst = conn.prepareStatement(sql);
pst.setString(1, a1.getText());
pst.setString(2,a2.getText());
pst.setString(3, a3.getText());
pst.setString(4, a4.getText());
pst.setString(5, a5.getText());
pst.setString(6, a6.getText());
pst.setString(7, a7.getText());
pst.setString(8, a8.getText());
pst.setString(9, a9.getText());
pst.setString(10, a10.getText());
pst.setString(11, a11.getText());
pst.setString(12, a12.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Saved");
//UpdateJTableSynola();
// conn.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex);
}
And now the problem is the date from the a2.getText() field which i put into database as (dd/MM/yyyy).The error is unparseable date.How can i fix this?
As JDBC-ODBC Bridge has been removed in JDK8, what you can do is to use JDBC along with UCanAccess API to connect to an MSAccess database.
connection = DriverManager
.getConnection("jdbc:ucanaccess:////REMOTE-IP-ADDRESS/shared-folder/TestDB.mdb");
System.out.println("CONNECTION ESTABLISHED....");
Here is a small example(tested in Java 8) which connects to a remote MS-Access DB using JDBC/UCanAccess API. Make sure you have the following jars in your project build path.
commons-lang-2.6.jar
commons-logging-1.1.1.jar
hsqldb.jar
jackcess-2.1.0.jar
ucanaccess-2.0.9.5.jar
Code:
/**
* Connects to a remote MS-Access DB using JDBC/UCanAccess API.
*/
package miscellaneous;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ConnectRemoteDB {
public static void main(String[] args) {
initializeConnection();
}
/**
* Initializes remote database connection and inserts a record and closes the connection.
*/
private static void initializeConnection() {
System.out.println("Attempting Database Connection...");
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DriverManager
.getConnection("jdbc:ucanaccess:////REMOTE-IP-ADDRESS/shared-folder/TestDB.mdb");
System.out.println("CONNECTION ESTABLISHED....");
String insertTableSQL = "INSERT INTO Table1" + "(Name) VALUES"
+ "(?)";
preparedStatement = connection.prepareStatement(insertTableSQL);
preparedStatement.setString(1, "Sandeep");
preparedStatement.executeUpdate();
System.out.println("RECORD INSERTED...");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
connection.close();
System.out.println("CONNECTION CLOSED...");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

Validate username and password in phpmyAdmin

I am trying to validate a database (phpMyadmin) using Username and Password. I need to search in all the tables (25 tables in my database) for checking if the given username and password are present (Authentication) or not. Can anyone provide the query to search entire table using the given username and password?
Below is my code
public class Dbclass {
/**
* #param args
*/
Connection conn = null;
Statement statement = null;
ResultSet resultSet = null;
ResultSetMetaData metaData = null;
String DB_URL="com.mysql.jdbc.Driver";
String USER="java";
String PASS="redhat";
String username;
String password;
ResultSet rs;
public ResultSet dbConnection(String query)
{
//STEP 2: Register JDBC driver
try {
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
try {
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
Statement stmt = conn.createStatement();
//sql = "SELECT username,password FROM Employees";
rs = stmt.executeQuery(query);
while(rs.next()){
//Retrieve by column name
username = rs.getString("user_name");
password = rs.getString("password");
//Display values
System.out.print("User_name: " + username);
System.out.print(",Password: " + password);
}
Authentication obj=new Authentication();
obj.userLogin(username,password);
//STEP 6: Clean-up environment
//rs.close();
//return rs;
stmt.close();
conn.close();
} catch (SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException es) {
//TODO Auto-generated catch block
es.printStackTrace();
}
finally{
//finally block used to close resources
try{
if(conn!=null){
conn.close();
}
if (rs != null) {
rs.close();
}
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
return rs;
}
}
I guess you're looking for a where clause. Something like below.
SELECT username,password FROM Employees where username='usernm' and password='pwd'
If required, you can add UNION to merge more such queries, but not recommended due to performance issues.

Android Communications link failure when minSdkVersion is greater than 9

Using the mysql-connector-java.5.1.26-bin.jar to connect my android app to a remote mysql host. Problem is, anything above minSdkVersion 9 casuses the "Communications link failure". If I have it set to 9, it connects fine and gets database information. Here's my connection code:
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from users");
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()) {
....
}
}
catch(Exception e) {
e.printStackTrace();
}
Any ideas as to why this is happening?
Turns out the problem was that I was trying to do a network connection in the main activity. For those that might have the same problem, look into using asynctask to complete the connection.
http://developer.android.com/reference/android/os/AsyncTask.html
Here is my Development Environment:
OS: Ubuntu14.04
IDE: Eclipse Luna
DB: MySQL5.1.73
Firstly,I put the code below in OnCreate method:
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, username,
password);
Statement stmt = conn.createStatement();
String sql = "select distinct Action from attributes";
ResultSet rs = stmt.executeQuery(sql);
The connection code is OK, But It will occur:
communication link failure.The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
So it must be wrong in something else. After trying various method, and solve it using android AsyncTask. Just put the code relative to operating mysql in Class that extends AsyncTask.
Below is my code:
private class Connect extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... urls) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, username,
password);
Statement stmt = conn.createStatement();
String sql = "select distinct Action from attributes";
ResultSet rs = stmt.executeQuery(sql);
List<String> actions = new ArrayList<String>();
while (rs.next()) {
actions.add(rs.getString("Action"));
}
} catch (Exception e) {
Log.d("MySQLConnection", e.getMessage());
}
return null;
}
}

Categories