Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ServerApplet extends javax.swing.JApplet {
- String word;
- ServerApplet applet;
- Connection con;
- @Override
- public void init() {
- try {
- java.awt.EventQueue.invokeAndWait(new Runnable() {
- public void run() {
- initComponents();
- applet = new ServerApplet();
- }
- });
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public Connection setConnection() {
- try {
- Class.forName("com.mysql.jdbc.Driver"); //Load correct driver
- } catch (ClassNotFoundException ex) {
- //Logger.getLogger(connection.class.getName()).log(Level.SEVERE, null, ex);
- }
- String URL = "jdbc:mysql://91.100.103.91:3306/test";
- String USERNAME = "test";
- String PASSWORD = "test1234";
- try {
- Connection n = DriverManager.getConnection(URL, USERNAME, PASSWORD);
- if (n != null) {
- System.out.println("Connection good");
- return n;
- }
- } catch (SQLException ex) {
- System.out.println("numse");
- //Logger.getLogger(connection.class.getName()).log(Level.SEVERE, null, ex);
- }
- return null;
- }
- public void addNewItem(String sendWord) { //Adds a new item tot he database
- con = this.setConnection();
- String query = "INSERT INTO test.\"testtable\" (\"word\")" + "VALUES (?)";
- try {
- if (con != null) {
- PreparedStatement preparedStatement = con.prepareStatement(query);
- preparedStatement.setString(1, sendWord); //Appends wildcard (%) to query variable
- preparedStatement.execute();
- /* Could return true if insert command is succesful
- if(preparedStatement.execute()){
- flag = true;
- }
- */
- preparedStatement.close();
- } else {
- jLabel3.setText("<< connection >>");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- System.out.println("NOOO!");
- } finally {
- if (con != null) {
- try {
- con.close();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
- public void getItem() {
- String query = "SELECT * FROM test.\"testtable\"";
- try {
- con = applet.setConnection();
- PreparedStatement preparedStatement;
- preparedStatement = con.prepareStatement(query);
- ResultSet resultSet = preparedStatement.executeQuery();
- while (resultSet.next()) {
- jLabel3.setText(resultSet.toString());
- }
- preparedStatement.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if (con != null) {
- try {
- con.close();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment